(Windows 筛选平台) 创建设备对象

标注驱动程序必须先创建设备对象,然后才能将其标注注册到筛选器引擎。 标注驱动程序如何创建设备对象取决于标注驱动程序是基于 Windows 驱动程序模型 (WDM) 还是 Windows 驱动程序框架 (WDF) 。

WDM-Based标注驱动程序

如果标注驱动程序基于 WDM,它将通过调用 IoCreateDevice 函数创建设备对象。 例如:

PDEVICE_OBJECT deviceObject;

NTSTATUS
 DriverEntry(
    IN PDRIVER_OBJECT DriverObject,
    IN PUNICODE_STRING RegistryPath
    )
{
  NTSTATUS status;

  ...

  // Create a device object
 status =
 IoCreateDevice(
 DriverObject,
      0,
      NULL,
      FILE_DEVICE_UNKNOWN,
      FILE_DEVICE_SECURE_OPEN,
      FALSE,
      &deviceObject
      );

  ...

 return status;
}

WDF-Based标注驱动程序

如果标注驱动程序基于 WDF,它将通过调用 WdfDeviceCreate 函数创建框架设备对象。 若要向筛选器引擎注册其标注,基于 WDF 的标注驱动程序必须获取指向与框架设备对象关联的 WDM 设备对象的指针。 基于 WDF 的标注驱动程序通过调用 WdfDeviceWdmGetDeviceObject 函数获取指向此 WDM 设备对象的指针。 例如:

WDFDEVICE wdfDevice;

NTSTATUS
 DriverEntry(
    IN PDRIVER_OBJECT DriverObject,
    IN PUNICODE_STRING RegistryPath
    )
{
  WDFDRIVER driver;
  PWDFDEVICE_INIT deviceInit;
  PDEVICE_OBJECT deviceObject;
  NTSTATUS status;

  ...

  // Allocate a device initialization structure
 deviceInit =
 WdfControlDeviceInitAllocate(
    driver;
    &SDDL_DEVOBJ_KERNEL_ONLY
    );

  // Set the device characteristics
 WdfDeviceInitSetCharacteristics(
    deviceInit,
    FILE_DEVICE_SECURE_OPEN,
    FALSE
    );

  // Create a framework device object
 status =
 WdfDeviceCreate(
    &deviceInit,
    WDF_NO_OBJECT_ATTRIBUTES,
    &wdfDevice
    );

  // Check status
 if (status == STATUS_SUCCESS) {

    // Initialization of the framework device object is complete
    WdfControlFinishInitializing(
      wdfDevice
    );

    // Get the associated WDM device object
    deviceObject = WdfDeviceWdmGetDeviceObject(wdfDevice);
  }

  ...

 return status;
}