上篇《Windows 驱动开发 - 3》我们使用了PnP,现在还差WMI。

    WMI:Windows Management Interface

    WMI 提供预装的类架构,允许使用脚本语言(VBS)、C#、VB .NET 或 C++ 编写的脚本或应用程序监视和配置计算机中的应用程序、系统或网络组件以及硬件。

一、WMI

    虽然WDF帮你完成很多工作,但是你还必须自己触发事件和实例化回调函数。

    在WDM中使用 wmilib.sys 静态注册很容易,但难以动态注册。

    但是在WDF中这些很容易实现。

    1. IRP_MJ_SYSTEM_CONTROL

        WDF完全掌控IRP_MJ_SYSTEM_CONTROL。

        IRP_MJ_SYSTEM_CONTROL:系统内部产生的控制信息,类似于内核调用DeviceControl函数

        注意:使用DeviceControl系统最低要求为xp。


  1. BOOL WINAPI DeviceIoControl(
  2.   _In_ HANDLE hDevice,
  3.   _In_ DWORD dwIoControlCode,
  4.   _In_opt_ LPVOID lpInBuffer,
  5.   _In_ DWORD nInBufferSize,
  6.   _Out_opt_ LPVOID lpOutBuffer,
  7.   _In_ DWORD nOutBufferSize,
  8.   _Out_opt_ LPDWORD lpBytesReturned,
  9.   _Inout_opt_ LPOVERLAPPED lpOverlapped
  10. );

2. 模式


     (1) 实例化已用设备

     (2) 当需要的时候触发事件

     (3) 统计报告

     (4) 更新状态

     (5) 注销后删除设备 (正常或意外删除)


    3. ETW / 跟踪

        支持内核模式 ETW / 跟踪

二、WDF和WMI

     wdf使用WdfDeviceCreateDeviceInterface为WMI提供接口。


  1. NTSTATUS WdfDeviceCreateDeviceInterface(
  2.   [in] WDFDEVICE Device,
  3.   [in] const GUID *InterfaceClassGUID,
  4.   [in, optional] PCUNICODE_STRING ReferenceString
  5. );


    其中GUID,请参见“Using GUIDs in Drivers


    GUID声明(需要使用头文件initguid.h):


  1. DEFINE_GUID(GUID_DEVINTERFACE_OSRUSBFX2, // Generated using guidgen.exe
  2.    0x573e8c73, 0xcb4, 0x4471, 0xa1, 0xbf, 0xfa, 0xb2, 0x6c, 0x31, 0xd3, 0x84);
  3. // {573E8C73-0CB4-4471-A1BF-FAB26C31D384}
源代码step2.c

  1. /*++

  2. Step2: This steps shows:
  3.        1) How to create a context with the WDFDEVICE object
  4.        2) How to initialize the USB device.
  5.        3) How to register an interface so that app can open
  6.           an handle to the device.
  7. --*/

  8. #include "ntddk.h"
  9. #include "wdf.h"
  10. #include "prototypes.h"
  11. #pragma warning(disable:4200) // suppress nameless struct/union warning
  12. #pragma warning(disable:4201) // suppress nameless struct/union warning
  13. #pragma warning(disable:4214) // suppress bit field types other than int warning
  14. #include "usbdi.h"
  15. #pragma warning(default:4200)
  16. #pragma warning(default:4201)
  17. #pragma warning(default:4214)
  18. #include "wdfusb.h"
  19. #include "initguid.h"

  20. DEFINE_GUID(GUID_DEVINTERFACE_OSRUSBFX2, // Generated using guidgen.exe
  21.    0x573e8c73, 0xcb4, 0x4471, 0xa1, 0xbf, 0xfa, 0xb2, 0x6c, 0x31, 0xd3, 0x84);
  22. // {573E8C73-0CB4-4471-A1BF-FAB26C31D384}

  23. typedef struct _DEVICE_CONTEXT {
  24.   WDFUSBDEVICE UsbDevice;
  25.   WDFUSBINTERFACE UsbInterface;
  26. } DEVICE_CONTEXT, *PDEVICE_CONTEXT;

  27. WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(DEVICE_CONTEXT, GetDeviceContext)


  28. NTSTATUS
  29. DriverEntry(
  30.     IN PDRIVER_OBJECT DriverObject,
  31.     IN PUNICODE_STRING RegistryPath
  32.     )
  33. {
  34.     WDF_DRIVER_CONFIG config;
  35.     NTSTATUS status;

  36.     KdPrint(("DriverEntry of Step2\n"));

  37.     WDF_DRIVER_CONFIG_INIT(&config, EvtDeviceAdd);

  38.     status = WdfDriverCreate(DriverObject,
  39.                         RegistryPath,
  40.                         WDF_NO_OBJECT_ATTRIBUTES,
  41.                         &config,
  42.                         WDF_NO_HANDLE
  43.                         );

  44.     if (!NT_SUCCESS(status)) {
  45.         KdPrint(("WdfDriverCreate failed 0x%x\n", status));
  46.     }

  47.     return status;
  48. }

  49. NTSTATUS
  50. EvtDeviceAdd(
  51.     IN WDFDRIVER Driver,
  52.     IN PWDFDEVICE_INIT DeviceInit
  53.     )
  54. {
  55.     WDF_OBJECT_ATTRIBUTES attributes;
  56.     NTSTATUS status;
  57.     WDFDEVICE device;
  58.     PDEVICE_CONTEXT pDevContext;
  59.     WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
  60.     
  61.     UNREFERENCED_PARAMETER(Driver);

  62.     WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
  63.     pnpPowerCallbacks.EvtDevicePrepareHardware = EvtDevicePrepareHardware;
  64.     WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);

  65.     WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, DEVICE_CONTEXT);

  66.     status = WdfDeviceCreate(&DeviceInit, &attributes, &device);
  67.     if (!NT_SUCCESS(status)) {
  68.         KdPrint(("WdfDeviceCreate failed 0x%x\n", status));
  69.         return status;
  70.     }

  71.     pDevContext = GetDeviceContext(device);

  72.     status = WdfDeviceCreateDeviceInterface(device,
  73.                                 (LPGUID) &GUID_DEVINTERFACE_OSRUSBFX2,
  74.                                 NULL);// Reference String
  75.     if (!NT_SUCCESS(status)) {
  76.         KdPrint(("WdfDeviceCreateDeviceInterface failed 0x%x\n", status));
  77.         return status;
  78.     }

  79.     return status;
  80. }


  81. NTSTATUS
  82. EvtDevicePrepareHardware(
  83.     IN WDFDEVICE Device,
  84.     IN WDFCMRESLIST ResourceList,
  85.     IN WDFCMRESLIST ResourceListTranslated
  86.     )
  87. {
  88.     NTSTATUS status;
  89.     PDEVICE_CONTEXT pDeviceContext;
  90.     WDF_USB_DEVICE_SELECT_CONFIG_PARAMS configParams;

  91.     UNREFERENCED_PARAMETER(ResourceList);
  92.     UNREFERENCED_PARAMETER(ResourceListTranslated);

  93.     pDeviceContext = GetDeviceContext(Device);

  94.     //
  95.     // Create the USB device if it is not already created.
  96.     //
  97.     if (pDeviceContext->UsbDevice == NULL) {
  98.         status = WdfUsbTargetDeviceCreate(Device,
  99.                                     WDF_NO_OBJECT_ATTRIBUTES,
  100.                                     &pDeviceContext->UsbDevice);
  101.         if (!NT_SUCCESS(status)) {
  102.             KdPrint(("WdfUsbTargetDeviceCreate failed 0x%x\n", status));
  103.             return status;
  104.         }
  105.     }
  106.     
  107.     WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE(&configParams);

  108.     status = WdfUsbTargetDeviceSelectConfig(pDeviceContext->UsbDevice,
  109.                                         WDF_NO_OBJECT_ATTRIBUTES,
  110.                                         &configParams);
  111.     if(!NT_SUCCESS(status)) {
  112.         KdPrint(("WdfUsbTargetDeviceSelectConfig failed 0x%x\n", status));
  113.         return status;
  114.     }

  115.     pDeviceContext->UsbInterface =
  116.                 configParams.Types.SingleInterface.ConfiguredUsbInterface;

  117.     return status;
  118. }





10-03 15:57