编写 Hello World Windows 驱动程序 (KMDF)Write a Hello World Windows Driver (KMDF)

本主题介绍了如何使用内核模式驱动程序框架 (KMDF) 编写非常小的通用 Windows 驱动程序,然后在单独的计算机上部署并安装驱动程序。This topic describes how to write a very small Universal Windows driver using Kernel-Mode Driver Framework (KMDF) and then deploy and install your driver on a separate computer.

若要开始操作,请确保已安装 Microsoft Visual StudioWindows SDKWindows 驱动程序工具包 (WDK)To get started, be sure you have Microsoft Visual Studio, the Windows SDK, and the Windows Driver Kit (WDK) installed.

安装 WDK 时,需要包括 Windows 调试工具Debugging Tools for Windows is included when you install the WDK.

创建和生成驱动程序Create and build a driver

  1. 打开 Microsoft Visual Studio。Open Microsoft Visual Studio. 在“文件”菜单上,选择“新建”>“项目”。On the File menu, choose New > Project.

  2. 在“新建项目”对话框的左侧窗格中,依次转到“Visual C++”>“Windows 驱动程序”>“WDF”。In the New Project dialog box, in the left pane, go to Visual C++ > Windows Drivers > WDF.

  3. 在中间窗格中,选择“内核模式驱动程序,空(KMDF)”。In the middle pane, select Kernel Mode Driver, Empty (KMDF).

  4. 在“名称”字段中,输入“KmdfHelloWorld”作为项目名称。In the Name field, enter "KmdfHelloWorld" for the project name.

    备注

    在创建新的 KMDF 或 UMDF 驱动程序时,必须选择一个不多于 32 个字符的驱动程序名称。When you create a new KMDF or UMDF driver, you must select a driver name that has 32 characters or less. 此长度限制在 wdfglobals.h 中定义。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. 选择“确定”。Select OK.

    “新建项目”对话框的屏幕截图

    Visual Studio 将创建一个项目和一个解决方案。Visual Studio creates one project and a solution. 可以在“解决方案资源管理器”窗口中看到它们,如此处所示。You can see them in the Solution Explorer window, shown here. (如果未显示“解决方案资源管理器”窗口,请从“视图”菜单中选择“解决方案资源管理器”。)该解决方案有一个名为 KmdfHelloWorld 的驱动程序项目。(If the Solution Explorer window is not visible, choose Solution Explorer from the View menu.) The solution has a driver project named KmdfHelloWorld.

    “解决方案资源管理器”窗口的屏幕截图,显示了解决方案和空的驱动程序项目 (kmdfhelloworld)

  7. 在“解决方案资源管理器”窗口中,选择并按住(或右键单击)KmdfHelloWorld 项目,然后选择“属性” 。In the Solution Explorer window, select and hold (or right-click) the KmdfHelloWorld project and choose Properties. 导航到“配置属性”>“驱动程序设置”>“常规”。请注意,“目标平台”默认为“通用”。Navigate to Configuration Properties > Driver Settings > General, and note that Target Platform defaults to Universal. 选择“应用”,然后选择“确定” 。Select Apply, and then select OK..

  8. 在“解决方案资源管理器”窗口中,再再次选择并按住(或右键单击)KmdfHelloWorld 项目,选择“添加”,然后选择“新项” 。In the Solution Explorer window, again select and hold (or right-click) the KmdfHelloWorld project, choose Add, and then select New Item.

  9. 在“添加新项目”对话框中,选择“C++ 文件”。In the Add New Item dialog box, select C++ File. 对于“名称”,请输入“Driver.c”。For Name, enter "Driver.c".

    备注

    文件扩展名为 .c,不是 .cppThe file name extension is .c, not .cpp.

    选择 添加Select Add. Driver.c 文件添加在源文件下,如下所示。The Driver.c file is added under Source Files, as shown here.

    “解决方案资源管理器”窗口的屏幕截图,显示添加到驱动程序项目中的 driver.c 文件

编写第一个驱动程序代码Write your first driver code

创建空的 Hello World 项目并添加 Driver.c 源文件以后,即可通过实现两个基本事件回调函数来编写驱动程序运行所需的最基本的代码。Now that you've created your empty Hello World project and added the Driver.c source file, you'll write the most basic code necessary for the driver to run by implementing two basic event callback functions.

  1. 在 Driver.c 中,首先包括以下头文件:In Driver.c, start by including these headers:

    #include <ntddk.h>
    #include <wdf.h>
    

    提示

    如果无法添加 Ntddk.h,请打开“配置”->“C/C++”>“常规”>“其他包含目录”并添加 C:\Program Files (x86)\Windows Kits\10\Include\<build#>\km,将 <build#> 替换为 WDK 安装中的相应目录。If you can't add Ntddk.h, open Configuration -> C/C++ -> General -> Additional Include Directories and add C:\Program Files (x86)\Windows Kits\10\Include\<build#>\km, replacing <build#> with the appropriate directory in your WDK installation.

    Ntddk.h 包含所有驱动程序的核心 Windows 内核定义,而 Wdf.h 包含基于 Windows 驱动程序框架 (WDF) 的驱动程序的定义。Ntddk.h contains core Windows kernel definitions for all drivers, while Wdf.h contains definitions for drivers based on the Windows Driver Framework (WDF).

  2. 接下来,为要使用的两个回调提供声明:Next, provide declarations for the two callbacks you'll use:

    DRIVER_INITIALIZE DriverEntry;
    EVT_WDF_DRIVER_DEVICE_ADD KmdfHelloWorldEvtDeviceAdd;
    
  3. 使用以下代码编写 DriverEntryUse the following code to write your DriverEntry:

    NTSTATUS 
    DriverEntry(
        _In_ PDRIVER_OBJECT     DriverObject, 
        _In_ PUNICODE_STRING    RegistryPath
    )
    {
        // NTSTATUS variable to record success or failure
        NTSTATUS status = STATUS_SUCCESS;
    
        // Allocate the driver configuration object
        WDF_DRIVER_CONFIG config;
    
        // Print "Hello World" for DriverEntry
        KdPrintEx(( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: DriverEntry\n" ));
    
        // Initialize the driver configuration object to register the
        // entry point for the EvtDeviceAdd callback, KmdfHelloWorldEvtDeviceAdd
        WDF_DRIVER_CONFIG_INIT(&config, 
                               KmdfHelloWorldEvtDeviceAdd
                               );
    
        // Finally, create the driver object
        status = WdfDriverCreate(DriverObject, 
                                 RegistryPath, 
                                 WDF_NO_OBJECT_ATTRIBUTES, 
                                 &config, 
                                 WDF_NO_HANDLE
                                 );
        return status;
    }
    

    DriverEntry 是所有驱动程序的入口点,就像 Main() 适用于许多用户模式应用程序一样。DriverEntry is the entry point for all drivers, like Main() is for many user mode applications. DriverEntry 的任务是初始化驱动程序范围的结构和资源。The job of DriverEntry is to initialize driver-wide structures and resources. 在此示例中,你针对 DriverEntry 输出了“Hello World”,将驱动程序对象配置为注册 EvtDeviceAdd 回调的入口点,然后创建了驱动程序对象并返回。In this example, you printed "Hello World" for DriverEntry, configured the driver object to register your EvtDeviceAdd callback's entry point, then created the driver object and returned.

    驱动程序对象充当你可能在驱动程序中创建的所有其他框架对象的父对象,这些框架对象包括设备对象、I/O 队列、计时器、旋转锁等。The driver object acts as the parent object for all other framework objects you might create in your driver, which include device objects, I/O queues, timers, spinlocks, and more. 有关框架对象的详细信息,请参阅框架对象简介For more information about framework objects, see Introduction to Framework Objects.

    提示

    对于 DriverEntry,我们强烈建议将名称保留为“DriverEntry”,以便进行代码分析和调试。For DriverEntry, we strongly recommend keeping the name as "DriverEntry" to help with code analysis and debugging.

  4. 接下来,使用以下代码编写 KmdfHelloWorldEvtDeviceAddNext, use the following code to write your KmdfHelloWorldEvtDeviceAdd:

    NTSTATUS 
    KmdfHelloWorldEvtDeviceAdd(
        _In_    WDFDRIVER       Driver, 
        _Inout_ PWDFDEVICE_INIT DeviceInit
    )
    {
        // We're not using the driver object,
        // so we need to mark it as unreferenced
        UNREFERENCED_PARAMETER(Driver);
    
        NTSTATUS status;
    
        // Allocate the device object
        WDFDEVICE hDevice;    
    
        // Print "Hello World"
        KdPrintEx(( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: KmdfHelloWorldEvtDeviceAdd\n" ));
    
        // Create the device object
        status = WdfDeviceCreate(&DeviceInit, 
                                 WDF_NO_OBJECT_ATTRIBUTES,
                                 &hDevice
                                 );
        return status;
    }
    

    系统在检测到你的设备已到达时,会调用 EvtDeviceAddEvtDeviceAdd is invoked by the system when it detects that your device has arrived. 它的任务是初始化该设备的结构和资源。Its job is to initialize structures and resources for that device. 在此示例中,你仅针对 EvtDeviceAdd 输出了“Hello World”消息、创建了设备对象并返回。In this example, you simply printed out a "Hello World" message for EvtDeviceAdd, created the device object, and returned. 在你编写的其他驱动程序中,可以为硬件创建 I/O 队列,为特定于设备的信息设置设备上下文存储空间,或执行准备设备所需的其他任务。In other drivers you write, you might create I/O queues for your hardware, set up a device context storage space for device-specific information, or perform other tasks needed to prepare your device.

    提示

    对于设备添加回调,请注意以驱动程序名称为前缀对回调命名的方式 (KmdfHelloWorld EvtDeviceAdd)。For the device add callback, notice how you named it with your driver's name as a prefix (KmdfHelloWorld EvtDeviceAdd). 通常,我们建议以这种方式命名驱动程序功能,以区别于其他驱动程序的功能。Generally, we recommend naming your driver's functions in this way to differentiate them from other drivers' functions. DriverEntry 是完全应该这样命名的唯一一项。DriverEntry is the only one you should name exactly that.

  5. 现在,整个 Driver.c 如下所示:Your complete Driver.c now looks like this:

    #include <ntddk.h>
    #include <wdf.h>
    DRIVER_INITIALIZE DriverEntry;
    EVT_WDF_DRIVER_DEVICE_ADD KmdfHelloWorldEvtDeviceAdd;
    
    NTSTATUS 
    DriverEntry(
        _In_ PDRIVER_OBJECT     DriverObject, 
        _In_ PUNICODE_STRING    RegistryPath
    )
    {
        // NTSTATUS variable to record success or failure
        NTSTATUS status = STATUS_SUCCESS;
    
        // Allocate the driver configuration object
        WDF_DRIVER_CONFIG config;
    
        // Print "Hello World" for DriverEntry
        KdPrintEx(( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: DriverEntry\n" ));
    
        // Initialize the driver configuration object to register the
        // entry point for the EvtDeviceAdd callback, KmdfHelloWorldEvtDeviceAdd
        WDF_DRIVER_CONFIG_INIT(&config, 
                               KmdfHelloWorldEvtDeviceAdd
                               );
    
        // Finally, create the driver object
        status = WdfDriverCreate(DriverObject, 
                                 RegistryPath, 
                                 WDF_NO_OBJECT_ATTRIBUTES, 
                                 &config, 
                                 WDF_NO_HANDLE
                                 );
        return status;
    }
    
    NTSTATUS 
    KmdfHelloWorldEvtDeviceAdd(
        _In_    WDFDRIVER       Driver, 
        _Inout_ PWDFDEVICE_INIT DeviceInit
    )
    {
        // We're not using the driver object,
        // so we need to mark it as unreferenced
        UNREFERENCED_PARAMETER(Driver);
    
        NTSTATUS status;
    
        // Allocate the device object
        WDFDEVICE hDevice;    
    
        // Print "Hello World"
        KdPrintEx(( DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, "KmdfHelloWorld: KmdfHelloWorldEvtDeviceAdd\n" ));
    
        // Create the device object
        status = WdfDeviceCreate(&DeviceInit, 
                                 WDF_NO_OBJECT_ATTRIBUTES,
                                 &hDevice
                                 );
        return status;
    }
    
  6. 保存 Driver.c。Save Driver.c.

此示例说明了驱动程序的基本概念:驱动程序是一个“回调集合”,经初始化后,会在系统有需要时等待系统调用。This example illustrates a fundamental concept of drivers: they are a "collection of callbacks" that, once initialized, sit and wait for the system to call them when it needs something. 这可能是新设备到达事件、用户模式应用程序的 I/O 请求、系统电源关闭事件、另一个驱动程序的请求,或用户意外拔出设备时的意外删除事件。This could be a new device arrival event, an I/O request from a user mode application, a system power shutdown event, a request from another driver, or a surprise removal event when a user unplugs the device unexpectedly. 幸运的是,就“Hello World”而言,只需操心驱动程序和设备的创建。Fortunately, to say "Hello World," you only needed to worry about driver and device creation.

接下来,将生成驱动程序。Next, you'll build your driver.

生成驱动程序Build the driver

  1. 在“解决方案资源管理器”窗口中,选择并按住(或右键单击)“解决方案‘KmdfHelloWorld’(1 个项目)”,然后选择“配置管理器” 。In the Solution Explorer window, select and hold (or right-click) Solution 'KmdfHelloWorld' (1 project) and choose Configuration Manager. 为驱动程序项目选择配置和平台。Choose a configuration and platform for the driver project. 在本练习中,我们选择“调试”和“x64”。 For this exercise, we choose Debug and x64.

  2. 在“解决方案资源管理器”窗口中,选择并按住(或右键单击)KmdfHelloWorld,然后选择“属性” 。In the Solution Explorer window, select and hold (or right-click) KmdfHelloWorld and choose Properties. 在“Wpp 跟踪”>“所有选项”中,将“运行 Wpp 跟踪”设置为“否”。In Wpp Tracing > All Options, set Run Wpp tracing to No. 依次选择“应用”、“确定” 。Select Apply and then OK.

  3. 若要生成驱动程序,请从“生成”菜单中选择“生成解决方案”。To build your driver, choose Build Solution from the Build menu. Visual Studio 在“输出”窗口中显示生成进度。Visual Studio shows the build progress in the Output window. (如果“输出”窗口不可见,请从“视图”菜单中选择“输出”。)验证解决方案成功生成后,即可关闭 Visual Studio。(If the Output window is not visible, choose Output from the View menu.) When you have verified that the solution built successfully, you can close Visual Studio.

  4. 若要查看生成的驱动程序,则在“文件资源管理器”中,依次转到 KmdfHelloWorld 文件夹和 C:\KmdfHelloWorld\x64\Debug\KmdfHelloWorldTo see the built driver, in File Explorer, go to your KmdfHelloWorld folder, and then to C:\KmdfHelloWorld\x64\Debug\KmdfHelloWorld. 该文件夹包括:The folder includes:

    • KmdfHelloWorld.sys - 内核模式驱动程序文件KmdfHelloWorld.sys -- the kernel-mode driver file
    • KmdfHelloWorld.inf - 在安装驱动程序时 Windows 使用的信息文件KmdfHelloWorld.inf -- an information file that Windows uses when you install the driver
    • KmdfHelloWorld.cat - 安装程序验证驱动程序的测试签名所使用的目录文件KmdfHelloWorld.cat -- a catalog file that the installer uses to verify the driver's test signature

提示

如果在生成驱动程序时看到 DriverVer set to a date in the future,则请更改驱动程序项目设置,让 Inf2Cat 设置 /uselocaltimeIf you see DriverVer set to a date in the future when building your driver, change your driver project settings so that Inf2Cat sets /uselocaltime. 为此,请使用“配置属性”->“Inf2Cat”->“常规”->“使用本地时间”。To do so, use Configuration Properties->Inf2Cat->General->Use Local Time. 现在,Stampinf 和 Inf2Cat 都使用本地时间。Now both Stampinf and Inf2Cat use local time.

部署驱动程序Deploy 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 the host computer, and the computer that runs the driver is called the target computer. 目标计算机也称为“测试计算机” 。The target computer is also called the test computer.

到目前为止,你已在主计算机上使用 Visual Studio 生成了驱动程序。So far you've used Visual Studio to build a driver on the host computer. 现在,需要配置目标计算机。Now you need to configure a target computer.

  1. 按照预配计算机以便进行驱动程序部署和测试 (WDK 10) 中的说明进行操作。Follow the instructions in Provision a computer for driver deployment and testing (WDK 10).

    提示

    按照步骤使用网络电缆自动预配目标计算机时,请记下端口和密钥。When you follow the steps to provision the target computer automatically using a network cable, take note of the port and key. 以后,你将在调试步骤中使用它们。You'll use them later in the debugging step. 在此示例中,我们将使用 50000 作为端口,使用 1.2.3.4 作为密钥。In this example, we'll use 50000 as the port and 1.2.3.4 as the key.

    在实际的驱动程序调试方案中,我们建议使用 KDNET 生成的密钥。In real driver debugging scenarios, we recommend using a KDNET-generated key. 有关如何使用 KDNET 生成一个随机密钥的详细信息,请参阅调试驱动程序 - 分步实验室(Sysvad 内核模式)主题。For more information about how to use KDNET to generate a random key, see the Debug Drivers - Step by Step Lab (Sysvad Kernel Mode) topic.

  2. 在主计算机上,在 Visual Studio 中打开你的解决方案。On the host computer, open your solution in Visual Studio. 可以在 KmdfHelloWorld 文件夹中双击解决方案文件 KmdfHelloWorld.sln。You can double-click the solution file, KmdfHelloWorld.sln, in your KmdfHelloWorld folder.

  3. 在“解决方案资源管理器”窗口中,选择并按住(或右键单击)KmdfHelloWorld 项目,然后选择“属性” 。In the Solution Explorer window, select and hold (or right-click) the KmdfHelloWorld project, and choose Properties.

  4. 在“KmdfHelloWorld 属性页”窗口中,转到“配置属性”>“驱动程序安装”>“部署”,如下所示。In the KmdfHelloWorld Property Pages window, go to Configuration Properties > Driver Install > Deployment, as shown here.

  5. 选中“部署前删除以前的驱动程序版本”。Check Remove previous driver versions before deployment.

  6. 对于“目标设备名称”,请选择配置用于测试和调试的计算机名。For Target Device Name, select the name of the computer that you configured for testing and debugging. 在本练习中,我们使用名为 MyTestComputer 的计算机。In this exercise, we use a computer named MyTestComputer.

  7. 选择“硬件 ID 驱动程序更新”,然后输入驱动程序的硬件 ID。Select Hardware ID Driver Update, and enter the hardware ID for your driver. 在本练习中,硬件 ID 为 Root\KmdfHelloWorld。For this exercise, the hardware ID is Root\KmdfHelloWorld. 选择“确定”。Select OK.

    “kmdfhelloworld 属性页”窗口的屏幕截图,显示选择了“部署驱动程序安装”screen shot showing the kmdfhelloworld property pages window with the deployment driver install selected

    备注

    在本练习中,硬件 ID 不标识真实的硬件。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. 对于真实的硬件,不要选择“硬件 ID 驱动程序更新”,而要选择“安装并验证”。For real hardware, do not select Hardware ID Driver Update; instead, select Install and Verify. 你将在驱动程序的信息 (INF) 文件中看到硬件 ID。You'll see the hardware ID in your driver's information (INF) file. 在“解决方案资源管理器”窗口中,转到“KmdfHelloWorld”>“驱动程序文件”,然后双击 KmdfHelloWorld.inf。In the Solution Explorer window, go to KmdfHelloWorld > Driver Files, and double-click KmdfHelloWorld.inf. 硬件 ID 位于 [Standard.NT$ARCH$] 下。The hardware ID is located under [Standard.NT$ARCH$].

    [Standard.NT$ARCH$]
    %KmdfHelloWorld.DeviceDesc%=KmdfHelloWorld_Device, Root\KmdfHelloWorld
    
  8. 在“生成”菜单上,选择“部署解决方案”。On the Build menu, choose Deploy Solution. Visual Studio 会自动将安装和运行驱动程序所需的文件复制到目标计算机。Visual Studio automatically copies the files required to install and run the driver to the target computer. 此操作可能会需要一两分钟的时间。This may take a minute or two.

    部署驱动程序时,驱动程序文件将复制到测试计算机上的 %Systemdrive%\drivertest\drivers 文件夹。When you deploy a driver, the driver files are copied to the %Systemdrive%\drivertest\drivers folder on the test computer. 如果部署期间发生错误,可以查看这些文件是否已复制到测试计算机。If something goes wrong during deployment, you can check to see if the files are copied to the test computer. 请确认 .inf、.cat、测试证书和 .sys 文件以及其他任何必要的文件均位于 %systemdrive%\drivertest\drivers 文件夹中。Verify that the .inf, .cat, test cert, and .sys files, and any other necessary files, are present in the %systemdrive%\drivertest\drivers folder.

    有关部署驱动程序的详细信息,请参阅将驱动程序部署到测试计算机For more information about deploying drivers, see Deploying a Driver to a Test Computer.

安装驱动程序Install the driver

将 Hello World 驱动程序部署到目标计算机后,即可安装该驱动程序。With your Hello World driver deployed to the target computer, now you'll install the driver. 如果你之前使用“自动”选项通过 Visual Studio 预配了目标计算机,则在预配过程中,Visual Studio 会将目标计算机设置为运行测试签名驱动程序。When you previously provisioned the target computer with Visual Studio using the automatic option, Visual Studio set up the target computer to run test signed drivers as part of the provisioning process. 现在,你只需使用 DevCon 工具安装驱动程序即可。Now you just need to install the driver using the DevCon tool.

  1. 在主计算机上,导航到 WDK 安装中的“Tools”文件夹,然后找到 DevCon 工具。On the host computer, navigate to the Tools folder in your WDK installation and locate the DevCon tool. 例如,在以下文件夹中查看:For example, look in the following folder:

    C:\Program Files (x86)\Windows Kits\10\Tools\x64\devcon.exeC:\Program Files (x86)\Windows Kits\10\Tools\x64\devcon.exe

    将 DevCon 工具复制到远程计算机。Copy the DevCon tool to your remote computer.

  2. 在目标计算机上,导航到包含驱动程序文件的文件夹,然后运行 DevCon 工具,以安装驱动程序。On the target computer, install the driver by navigating to the folder containing the driver files, then running the DevCon tool.

    1. 以下是将用于安装驱动程序的 devcon 工具的常规语法:Here's the general syntax for the devcon tool that you will use to install the driver:

      devcon install <INF file> <hardware ID>devcon install <INF file> <hardware ID>

      安装此驱动程序所需的 INF 文件是 KmdfHelloWorld.inf。The INF file required for installing this driver is KmdfHelloWorld.inf. INF 文件包含用于安装驱动程序二进制文件 KmdfHelloWorld.sys 的硬件 ID。The INF file contains the hardware ID for installing the driver binary, KmdfHelloWorld.sys. 回想一下,位于 INF 文件中的硬件 ID 是 Root\KmdfHelloWorldRecall that the hardware ID, located in the INF file, is Root\KmdfHelloWorld.

    2. 以管理员身份打开命令提示符窗口。Open a Command Prompt window as Administrator. 导航到内置驱动程序 .sys 文件所在的文件夹,然后输入以下命令:Navigate to your folder containing the built driver .sys file and enter this command:

      devcon install kmdfhelloworld.inf root\kmdfhelloworlddevcon install kmdfhelloworld.inf root\kmdfhelloworld

      如果收到一条关于 devcon 未被识别的错误消息,请尝试添加 devcon 工具的路径。If you get an error message about devcon not being recognized, try adding the path to the devcon tool. 例如,如果已将其复制到目标计算机上称为 C:\Tools 的文件夹,则尝试使用以下命令:For example, if you copied it to a folder on the target computer called C:\Tools, then try using the following command:

      c:\tools\devcon install kmdfhelloworld.inf root\kmdfhelloworldc:\tools\devcon install kmdfhelloworld.inf root\kmdfhelloworld

      此时将显示一个对话框,指示测试驱动程序是未签名驱动程序。A dialog box will appear indicating that the test driver is an unsigned driver. 选择“仍然安装此驱动程序”以继续。Select Install this driver anyway to proceed.

      驱动程序安装警告的屏幕截图

调试驱动程序Debug the driver

现在,你已在目标计算机上安装了 KmdfHelloWorld 驱动程序,将从主计算机远程连接调试程序。Now that you have installed your KmdfHelloWorld driver on the target computer, you'll attach a debugger remotely from the host computer.

  1. 在主计算机上,以管理员身份打开命令提示符窗口。On the host computer, open a Command Prompt window as Administrator. 转到 WinDbg.exe 目录。Change to the WinDbg.exe directory. 我们将使用安装 Windows 工具包过程中安装的 Windows 驱动程序工具包 (WDK) 中的 x64 版本 WinDbg.exe。We will use the x64version of WinDbg.exe from the Windows Driver Kit (WDK) that was installed as part of the Windows kit installation. 下面是 WinDbg.exe 的默认路径:Here is the default path to WinDbg.exe:

    C:\Program Files (x86)\Windows Kits\10\Debuggers\x64C:\Program Files (x86)\Windows Kits\10\Debuggers\x64

  2. 使用以下命令启动 WinDbg 以连接到目标计算机上的内核调试会话。Launch WinDbg to connect to a kernel debug session on the target computer by using the following command. 端口和密钥的值应该与你预配目标计算机所使用的值相同。The value for the port and key should be the same as what you used to provision the target computer. 我们将使用 50000 作为端口,使用 1.2.3.4 作为密钥。我们在部署步骤中使用过这些值。We'll use 50000 for the port and 1.2.3.4 for the key, the values we used during the deploy step. K 标志指示这是内核调试会话。The k flag indicates that this is a kernel debug session.

    WinDbg -k net:port=50000,key=1.2.3.4WinDbg -k net:port=50000,key=1.2.3.4

  3. 在“调试”菜单上,选择“中断”。On the Debug menu, choose Break. 主计算机上的调试程序将中断目标计算机。The debugger on the host computer will break into the target computer. 在“调试程序命令”窗口中,你可以看到内核调试命令提示符:kd>In the Debugger Command window, you can see the kernel debugging command prompt: kd>.

  4. 此时,可以试验调试程序,方法是在 kd> 提示符处输入命令。At this point, you can experiment with the debugger by entering commands at the kd> prompt. 例如,可以尝试使用以下命令:For example, you could try these commands:

  5. 若要让目标计算机再次运行,请从“调试”菜单中选择“执行”,或者按“g”,然后按“Enter”。To let the target computer run again, choose Go from the Debug menu or press "g," then press "enter."

  6. 若要停止调试会话,请从“调试”菜单中选择“分离调试程序”。To stop the debugging session, choose Detach Debuggee from the Debug menu.

    重要

    请确保在退出调试程序之前使用“执行”命令让目标计算机再次运行,否则目标计算机将仍然对你的鼠标和键盘输入无响应,因为它仍在与调试程序通话。Make sure you use the "go" command to let the target computer run again before exiting the debugger, or the target computer will remain unresponsive to your mouse and keyboard input because it is still talking to the debugger.

有关驱动程序调试过程的详细分步演练,请参阅调试通用驱动程序 - 分步实验室(回显内核模式)For a detailed step-by-step walkthrough of the driver debugging process, see Debug Universal Drivers - Step by Step Lab (Echo Kernel-Mode).

有关远程调试的详细信息,请参阅使用 WinDbg 远程调试For more information about remote debugging, see Remote Debugging Using WinDbg.

相关主题Related topics

开发、测试以及部署驱动程序Developing, Testing, and Deploying Drivers

Windows 调试工具Debugging Tools for Windows

调试通用驱动程序 - 分步实验室(回显内核模式)Debug Universal Drivers - Step by Step Lab (Echo Kernel-Mode)

编写你的第一个驱动程序Write your first driver