Write a KMDF driver based on a template

To write a Kernel-Mode Driver Framework (KMDF) driver, you can use a Microsoft Visual Studio template. To get started, be sure you have Visual Studio and the Windows Driver Kit (WDK) installed. You can download them from the Windows Hardware Dev Center.

Debugging Tools for Windows is included when you install the WDK.

Create and build a driver package

  1. Open Microsoft Visual Studio. On the File menu, choose New > Project. The New Project dialog box opens, as shown here.
  2. In the New Project dialog box, select WDF.
  3. In the middle pane, select Kernel Mode Driver (KMDF).
  4. In the Name field, enter "KmdfDriver" as the project name.

    Note  When you create a new KMDF or UMDF driver, you must select a driver name that has 32 characters or less. This length limit is defined in wdfglobals.h.

  5. In the Location field, enter the directory where you want to create the new project.
  6. Check Create directory for solution. Click OK.

    Screen shot of the New Project dialog box, showing WDF and Kernel Mode Driver selected

    Visual Studio creates two projects and a solution. You can them in the Solution Explorer window, as shown here. (If the Solution Explorer window is not visible, choose Solution Explorer from the View menu.) The solution has a driver project named KmdfDriver and a driver package project named KmdfDriver Package. To see the driver source code, open any of the files under Source Files. Driver.c and Device.c are good places to start.

    Screen shot of Solution Explorer showing the files in the driver project and the package project
  7. In the Solution Explorer window, right-click Solution 'KmdfDriver' (2 projects), and choose Configuration Manager. Choose a configuration and platform for both the driver project and the package project. In this exercise, we choose Win 8.1 Debug and x64.

  8. To build your driver and create a driver package, choose Build Solution from the Build menu. Visual Studio shows the build progress in the Output window. (If the Output window is not visible, choose Output from the View menu.)

    When you've verified that the solution built successfully, you can close Visual Studio.

  9. To see the built driver package, in File Explorer, go to your KmdfDriver folder, and then to x64\Win8.1Debug\KmdfDriver Package. The driver package has several files:

    • KmdfDriver.sys -- the kernel-mode driver file
    • KmdfDriver.inf -- an information file that Windows uses when you install the driver
    • KmdfDriver.cat -- a catalog file that the installer uses to verify the test signature for the driver package

    There might also be a coinstaller file in the package.

Deploy and install the driver

Typically when you test and debug a driver, the debugger and driver run on separate computers. The computer that runs the debugger is called the host computer, and the computer that runs the driver is called the target computer. The target computer is also called the test computer. For more information about debugging drivers, see Debugging Tools for Windows.

So far you've used Visual Studio to build a driver on the host computer. Now you need to configure a target computer. ToFollow the instructions in Provision a computer for driver deployment and testing (WDK 8.1). Then you can deploy, install, load, and debug your driver:

  1. On the host computer, open your solution in Visual Studio. You can double-click the solution file, KmdfDriver.sln, in your KmdfDriver folder.
  2. In the Solution Explorer window, right-click KmdfDriver Package, and choose Properties.
  3. In the KmdfDriver Package Property Pages window, in the left pane, go to Configuration Properties > Driver Install > Deployment.
  4. Check Enable deployment, and check Remove previous driver versions before deployment.
  5. For Remote Computer Name, select the name of the computer that you configured for testing and debugging. In this exercise, we use a computer named MyTestComputer.
  6. Select Hardware ID Driver Update, and enter the hardware ID for your driver. In this exercise, the hardware ID is Root\KmdfDriver. Click OK.

    Screen shot of the KmdfDriver Package Property Pages window, showing Deployment Driver Install selected

    Note  In this exercise, the hardware ID does not identify a real piece of hardware. It identifies an imaginary device that will be given a place in the device tree as a child of the root node. For real hardware, do not select Hardware ID Driver Update; instead, select Install and Verify.

    You'll see the hardware ID in your driver's information (INF) file. In the Solution Explorer window, go to KmdfDriver > Driver Files and double-click KmdfDriver.inf. The hardware ID is located under [Standard.NT$ARCH$].

    %KmdfDriver.DeviceDesc%=KmdfDriver_Device, Root\KmdfDriver
  7. On the Debug menu, choose Start Debugging, or press F5 on the keyboard.
  8. Visual Studio first shows progress in the Output window. Then it opens the Debugger Immediate Window and continues to show progress.

    Wait until your driver has been deployed, installed, and loaded on the target computer. This might take a minute or two.

  9. On the Debug menu, choose Break All. The debugger on the host computer will break into the target computer. In the Debugger Immediate Window, you'll see the kernel debugging command prompt: kd>.

    Screen shot of the command prompt in the Debugger Immediate window
  10. At this point, you can experiment with the debugger by entering commands at the kd> prompt. For example, you could try these commands:

  11. To let the target computer run again, choose Continue from the Debug menu.
  12. To stop the debugging session, choose Stop Debugging from the Debug menu.

Related topics

Developing, Testing, and Deploying Drivers
Debugging Tools for Windows
Write your first driver



Send comments about this topic to Microsoft

© 2014 Microsoft