Writing a very small KMDF driver

Writing a very small KMDF driver

ExpandMinimize
5 out of 6 rated this helpful- Rate this topic

[This documentation is preliminary and is subject to change.]

To create and test a Kernel-Mode Driver Framework (KMDF) driver, you need the Windows Driver Kit (WDK), which is integrated with Microsoft Visual Studio 2012 RC, and Debugging Tools for Windows. You can use a Microsoft Visual Studio template as a starting point.

For information about how to get the WDK and Visual Studio 2012 RC, see Windows Driver Kit 8.

For information about how to get Debugging Tools for Windows, see Download and Install Debugging Tools for Windows.

Creating and building a driver package

To create and build a small KMDF driver, follow these steps:

  1. Open Microsoft Visual Studio. On the File menu, choose New > Project. The New Project dialog box opens, as shown in the following screen shot.
  2. In the New Project dialog box, in the left pane, locate and selectWDF.
  3. In the middle pane, select Kernel Mode Driver, Empty (KMDF).
  4. In the Name field, enter "KmdfSmall" for the project name.
  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

    Visual Studio creates two projects and a solution. You can see the solution, the two projects, and the files that belong to each project in theSolution Explorer window, shown in the following screen shot. (If the Solution Explorer window is not visible, chooseSolution Explorer from the View menu.) The solution contains a driver project named KmdfSmall and a driver package project named KmdfSmall Package.

    Screen shot of the Solution Explorer window, showing the package project (KmdfSmall Packages) and the empty driver project (KmdfSmall)
  7. In the Solution Explorer window, right-click KmdfSmall and chooseAdd > New Item.
  8. In the Add New Item dialog box, select C++ File. ForName, enter "Driver.c".

    Note The file name extension is .c, not.cpp.

    Click Add. The Driver.c file is added under Source Files, as shown in the following screen shot.

    Screen shot of the Solution Explorer window, showing the Driver.c file added to the driver project
  9. Open Driver.c, and enter this code:

    C++
    #include <ntddk.h>
    #include <wdf.h>
    DRIVER_INITIALIZE DriverEntry;
    EVT_WDF_DRIVER_DEVICE_ADD KmdfSmallEvtDeviceAdd;
    
    NTSTATUS DriverEntry(PDRIVER_OBJECT  DriverObject, PUNICODE_STRING RegistryPath)
    {
        NTSTATUS status = STATUS_SUCCESS;
        WDF_DRIVER_CONFIG config = {0};
     
        KdPrintEx(( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfSmall: DriverEntry\n" ));
        WDF_DRIVER_CONFIG_INIT(&config, KmdfSmallEvtDeviceAdd);
        status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE);
        return status;
    }
    
    NTSTATUS KmdfSmallEvtDeviceAdd(WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit)
    {
        NTSTATUS status = STATUS_SUCCESS;
        WDFDEVICE hDevice;
    
        KdPrintEx(( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfSmall: KmdfSmallEvtDeviceAdd\n" ));
        status = WdfDeviceCreate(&DeviceInit, WDF_NO_OBJECT_ATTRIBUTES, &hDevice);
        return status;
    }
    
    
  10. Save Driver.c.
  11. In the Solution Explorer window, right-click Solution 'KmdfSmall' (2 projects) and chooseConfiguration Manager. Choose a configuration and platform for both the driver project and the package project. For this exercise, we choose Win7 Debug and x64, as shown in the following screen shot.

    Screen shot of the Configuration Manager window with Win7 Debug and x64 selected
  12. In the Solution Explorer window, right-click KmdfSmall and chooseProperties. In Wpp Tracing > All Options, set Run Wpp tracing to No. Click OK.
  13. To build your driver and create a driver package, choose Build Solution from theBuild menu. Visual Studio displays the build progress in the Output window, as shown in the following screen shot. (If the Output window is not visible, chooseOutput from the View menu.)

    Screen shot of the Output window, showing the final output of a successful build

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

  14. To see the built driver package, navigate in Windows Explorer to your KmdfSmall folder, and then to x64\Win7Debug\KmdfSmall Package. The driver package contains several files: KmdfSmall.sys is the kernel-mode driver file, KmdfSmall.inf is an information file that Windows uses when you install the driver, and KmdfSmall.cat is a catalog file that the installer uses to verify the test signature for the driver package. The other file is a co-installer for theWindows Driver Frameworks (WDF). These files are shown in the following screen shot.

    Screen shot of the driver package folder

Deploying and installing the driver

Typically when you test and debug a driver, the debugger and the driver run on separate computers. The computer that runs the debugger is called thehost computer, and the computer that runs the driver is called the target computer. The target computer is also called thetest computer.

So far in this exercise, you've used Visual Studio on the host computer to build a driver. Next you need to configure a target computer. To configure a target computer, follow the instructions inConfiguring a Computer for Testing and Debugging. After you have configured a target computer, you can deploy, install, load, and debug your driver by following these steps:

  1. On the host computer, open your solution in Visual Studio. One way to do this is to double-click the solution file, KmdfSmall.sln, in your KmdfSmall folder.
  2. In the Solution Explorer window, right-click KmdfSmall Package, and chooseProperties.
  3. In the KmdfSmall Package Property Pages window, in the left pane, navigate toConfiguration Properties > Driver Install > Deployment, as shown in the following screen shot.
  4. Check Enable deployment, and check Import into driver store.
  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. For this exercise, the hardware ID is Root\KmdfSmall. ClickOK.

    Screen shot showing the KmdfSmall Package Property Pages window with the 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 thedevice 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 theSolution Explorer window, navigate to KmdfSmall > Driver Files, and double-click KmdfSmall.inf. The hardware ID is located under [Standard.NT$ARCH$].

    [Standard.NT$ARCH$]
    %KmdfSmall.DeviceDesc%=KmdfSmall_Device, Root\KmdfSmall
    
    
  7. On the Debug menu, choose Start Debugging, or pressF5 on the keyboard.
  8. Visual Studio first displays progress in the Output window. Then it opens theDebugger Immediate window and continues to display progress, as shown in the following screen shot.

    Screen shot of the Debugger Immediate window

    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 theDebugger Immediate window, you can see the kernel debugging command prompt:kd>.

    Screen shot of the command prompt in the Debugger Immediate window
  10. On the Debug menu, choose Windows > Modules. Verify that KmdfSmall.sys is in the list of modules that are loaded on the target computer, as shown in the following screen shot.

    Screen shot of the Modules window with KmdfSmall.sys highlighted
  11. At this point, you can experiment with the debugger by entering commands at thekd> prompt. For example, you could try these commands:

  12. To let the target computer run again, choose Continue from theDebug menu.
  13. To stop the debugging session, choose Stop Debugging from theDebug menu. 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值