基于模板编写通用 Windows 驱动程序 (UMDF 2)Write a Universal Windows driver (UMDF 2) based on a template

本主题介绍了如何使用用户模式驱动程序框架 (UMDF) 2 编写通用 Windows 驱动程序This topic describes how to write a Universal Windows driver using User-Mode Driver Framework (UMDF) 2. 首先使用 Microsoft Visual Studio 模板,然后在单独的计算机上部署和安装驱动程序。You'll start with a Microsoft Visual Studio template and then deploy and install your driver on a separate computer.

若要开始操作,请确保已安装最新版本的 Microsoft Visual Studio 和 Windows 驱动程序工具包 (WDK)。To get started, be sure you have the most recent version of Microsoft Visual Studio and the Windows Driver Kit (WDK). 有关下载链接,请参阅下载 Windows 驱动程序工具包 (WDK)For download links, see Download the Windows Driver Kit (WDK).

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

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

备注

在创建新的 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.

  1. 打开 Visual Studio。Open 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. 选择“用户模式驱动程序(UMDF V2)” 。Select User Mode Driver (UMDF V2).

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

  4. 在“位置” 字段中,输入要在其中创建新项目的目录。In the Location field, enter the directory where you want to create the new project.

  5. 选中“创建解决方案的目录” 。Check Create directory for solution. 单击“确定” 。Click OK.

    “新建项目”对话框的屏幕截图,其中显示选中的 WDF 和用户模式驱动程序screen shot of the new project dialog box, showing wdf and user mode driver selected

    Visual Studio 将创建一个项目和一个解决方案。Visual Studio creates one project and a solution. 可以在“解决方案资源管理器” 窗口中看到它们。You can see them in the Solution Explorer window. (如果未显示“解决方案资源管理器” 窗口,请从“视图” 菜单中选择“解决方案资源管理器”。)该解决方案有一个名为 UmdfDriver 的驱动程序项目。(If the Solution Explorer window is not visible, choose Solution Explorer from the View menu.) The solution has a driver project named UmdfDriver. 若要查看驱动程序源代码,请打开源文件下的任何文件。To see the driver source code, open any of the files under Source Files. 可以先从 Driver.c 和 Device.c 开始。Driver.c and Device.c are good places to start.

    解决方案资源管理器的屏幕截图,其中显示驱动程序项目中的文件

  6. 在“解决方案资源管理器” 窗口中,右键单击“解决方案‘UmdfDriver’(1 个项目)” ,然后选择“配置管理器” 。In the Solution Explorer window, right-click Solution 'UmdfDriver' (1 project), and choose Configuration Manager. 为驱动程序项目选择配置和平台。Choose a configuration and platform for the driver project. 例如,选择“调试” 和“x64” 。For example, choose Debug and x64.

  7. 在“解决方案资源管理器” 窗口中,右键单击“UmdfDriver” ,然后选择“属性” 。In the Solution Explorer window, right-click UmdfDriver, and choose Properties. 导航到 “配置属性”>“驱动程序设置”>“常规”。请注意,“目标平台” 默认为“通用” 。Navigate to Configuration Properties > Driver Settings > General, and note that Target Platform defaults to Universal.

  8. 若要生成驱动程序,请从“生成” 菜单中选择“生成解决方案” 。To build your driver, choose Build Solution from the Build menu. Microsoft Visual Studio 将在“输出” 窗口中显示生成进度。Microsoft Visual Studio displays build progress in the Output window. (如果“输出” 窗口不可见,请从“视图” 菜单中选择“输出” 。)(If the Output window is not visible, choose Output from the View menu.)

    验证生成输出是否包括:Verify that the build output includes:

    >  Driver is a Universal Driver.
    

    确认解决方案成功生成后,可以关闭 Visual Studio。When you've verified that the solution built successfully, you can close Visual Studio.

  9. 若要查看生成的驱动程序,请在“文件资源管理器”中,依次转到 UmdfDriver 文件夹和 x64\Debug\UmdfDriverTo see the built driver, in File Explorer, go to your UmdfDriver folder, and then to x64\Debug\UmdfDriver. 该目录包含以下文件:The directory includes the following files:

    • UmdfDriver.dll -- 用户模式驱动程序文件UmdfDriver.dll -- the user-mode driver file
    • UmdfDriver.inf -- 在安装驱动程序时 Windows 使用的信息文件UmdfDriver.inf -- an information file that Windows uses when you install the driver

部署和安装通用 Windows 驱动程序Deploy and install the Universal Windows 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.

到目前为止,你已在主计算机上使用 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. 按照预配计算机以便进行驱动程序部署和测试 (WDK 10) 中的说明进行操作。Follow the instructions in Provision a computer for driver deployment and testing (WDK 10). 然后,你可以随时部署、安装、加载和调试驱动程序:Then you'll be ready to deploy, install, load, and debug your driver:

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

  2. 在“解决方案资源管理器” 窗口中,右键单击“UmdfDriver” ,然后选择“属性” 。In the Solution Explorer window, right-click UmdfDriver, and choose Properties.

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

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

  5. 对于“目标设备名称” ,请选择配置用于测试和调试的计算机名。For Target Device Name, select the name of the computer that you configured for testing and debugging.

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

    “umdfdriver 属性页”的屏幕截图,其中显示选择了“部署驱动程序安装”

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

    [Standard.NT$ARCH$]
    %DeviceName%=MyDevice_Install,Root\UmdfDriver
    
  7. 在“调试” 菜单上,选择“开始调试” 或按键盘上的 F5On the Debug menu, choose Start Debugging, or press F5 on the keyboard.

  8. 等待直至已在目标计算机上部署、安装以及加载驱动程序。Wait until your driver has been deployed, installed, and loaded on the target computer. 这可能需要几分钟的时间。This might take several minutes.

使用驱动程序模块框架 (DMF)Using the Driver Module Framework (DMF)

驱动程序模块 Framework (DMF) 是 WDF 的一个扩展,可为 WDF 驱动程序开发人员提供额外的功能。The Driver Module Framework (DMF) is an extension to WDF that enables extra functionality for a WDF driver developer. 它可以帮助开发人员更快、更好地编写任何类型的 WDF 驱动程序。It helps developers write any type of WDF driver better and faster.

作为一个框架,DMF 可用于创建称作“DMF 模块”的 WDF 对象。DMF as a framework allows creation of WDF objects called DMF Modules. 可以在不同的驱动程序之间共享这些 DMF 模块的代码。The code for these DMF Modules can be shared between different drivers. 此外,DMF 捆绑了为我们为驱动程序开发的 DMF 模块库,可为其他驱动程序开发人员提供价值。In addition, DMF bundles a library of DMF Modules that we have developed for our drivers and feel would provide value to other driver developers.

DMF 不会取代 WDF。DMF does not replace WDF. DMF 是与 WDF 搭配使用的另一个框架。DMF is a second framework that is used with WDF. 利用 DMF 的开发人员仍需使用 WDF 及其所有基元来编写设备驱动程序。The developer leveraging DMF still uses WDF and all its primitives to write device drivers.

有关详细信息,请参阅驱动程序模块框架 (DMF)For more info, see Driver Module Framework (DMF).

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

Windows 调试工具Debugging Tools for Windows

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