DRIVER_DISPATCH回调函数 (wdm.h)

回调例程服务各种 IRP。 有关函数代码的列表,请参阅“备注”。

语法

DRIVER_DISPATCH DriverDispatch;

NTSTATUS DriverDispatch(
  [in, out] _DEVICE_OBJECT *DeviceObject,
  [in, out] _IRP *Irp
)
{...}

参数

[in, out] DeviceObject

调用方提供的指向 DEVICE_OBJECT 结构的指针。 这是目标设备的设备对象,以前由驱动程序的 AddDevice 例程创建。

[in, out] Irp

调用方提供的指向描述所请求 I/O作的 IRP 结构的指针。

返回值

如果例程成功,则必须返回STATUS_SUCCESS。 否则,它必须返回 Ntstatus.h 中定义的错误状态值之一。

言论

所有 Dispatch 例程的输入参数都以 Irp指向的 IRP 结构提供。 其他参数在驱动程序的关联 I/O 堆栈位置中提供,该位置由 IO_STACK_LOCATION 结构描述,可通过 调用 ioGetCurrentIrpStackLocation来获取。

通常,所有 Dispatch 例程在 IRQL = PASSIVE_LEVEL 的任意线程上下文中执行,但存在例外情况。 有关详细信息,请参阅 调度例程和 IRCL

有关调度例程的详细信息,请参阅 编写调度例程。 有关 IRP 的详细信息,请参阅 处理 IRP

IRP 关于实现回调
IRP_MJ_CLEANUP 驱动程序的 DispatchCleanup 例程应命名为 XxxDispatchCleanup,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchCleanup 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_CLEANUP]。
IRP_MJ_CLOSE 驱动程序的 DispatchClose 例程应命名 为 XxxDispatchClose,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchClose 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_CLOSE]。
IRP_MJ_CREATE 驱动程序的 DispatchCreate 例程应命名为 XxxDispatchCreate,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchCreate 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_CREATE]。
IRP_MJ_CREATEIRP_MJ_CLOSE 驱动程序可以提供单个 DispatchCreateClose 例程,而不是单独的 DispatchCreateDispatchClose 例程。

驱动程序的 DispatchCreateClose 例程应命名为 Xxx XxxDispatchCreateClose,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchCreateClose 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_CREATE] 和 DriverObject->MajorFunction[IRP_MJ_CLOSE]。
IRP_MJ_DEVICE_CONTROL 驱动程序的 DispatchDeviceControl 例程应命名 为 XxxDispatchDeviceControl,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchDeviceControl 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]。

系统使用 I/O 控制代码中的 FILE_XXX 标志来确定 IRP 发送方是否有权将 IRP 发送到设备对象。 Windows Server 2003 及更高版本的 Windows 驱动程序可以使用 IoValidateDeviceIoControlAccess 例程在 DispatchDeviceControl中执行更严格的访问检查。
IRP_MJ_FLUSH_BUFFERS 驱动程序的 DispatchFlushBuffers 例程应命名为 XxxDispatchFlushBuffers,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchFlushBuffers 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS]。
IRP_MJ_INTERNAL_DEVICE_CONTROL 驱动程序的 DispatchInternalDeviceControl 例程应命名为 XxxDispatchInternalDeviceControl,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchInternalDeviceControl 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL]。
IRP_MJ_PNP 驱动程序的 DispatchPnP 例程应 XxxDispatchPnP 命名,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchPnP 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_PNP]。
IRP_MJ_POWER 驱动程序的 DispatchPower 例程应命名为 XxxDispatchPower,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchPower 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_POWER]。
IRP_MJ_QUERY_INFORMATION 驱动程序的 DispatchQueryInformation 例程应命名 为 XxxDispatchQueryInformation,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchQueryInformation 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION]。
IRP_MJ_READ 驱动程序的 DispatchRead 例程应命名为 XxxDispatchRead,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchRead 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_READ]。
IRP_MJ_READIRP_MJ_WRITE 驱动程序可以提供单个 DispatchReadWrite 例程,而不是单独的 DispatchReadDispatchWrite 例程。

驱动程序的 DispatchReadWrite 例程应命名为 XxxDispatchReadWrite,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchReadWrite 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_READ] 和 DriverObject->MajorFunction[IRP_MJ_WRITE]。
IRP_MJ_SET_INFORMATION 驱动程序的 DispatchSetInformation 例程应命名为 Xxx XxxDispatchSetInformation,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchSetInformation 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION]。
IRP_MJ_SHUTDOWN 驱动程序的 DispatchShutdown 例程应命名为 XxxDispatchShutdown,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchShutdown 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_SHUTDOWN]。

此外,为了接收 IRP_MJ_SHUTDOWN 请求,驱动程序必须调用 IoRegisterShutdownNotificationIoRegisterLastChanceShutdownNotification,以便向系统注册其 DispatchShutdown 例程。
IRP_MJ_SYSTEM_CONTROL 驱动程序的 DispatchSystemControl 例程应命名 为 XxxDispatchSystemControl,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchSystemControl 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL]。
IRP_MJ_WRITE 驱动程序的 DispatchWrite 例程应命名为 XxxDispatchWrite,其中 Xxx 是特定于驱动程序的前缀。 驱动程序的 DriverEntry 例程必须将 DispatchWrite 例程的地址存储在 DriverObject->MajorFunction[IRP_MJ_WRITE]。

例子

若要定义回调例程,必须先提供一个函数声明,用于标识要定义的回调例程的类型。 Windows 为驱动程序提供一组回调函数类型。 使用回调函数类型声明函数有助于 驱动程序代码分析静态驱动程序验证程序(SDV)和其他验证工具查找错误,这是为 Windows作系统编写驱动程序的要求。

例如,若要定义名为 MyDispatchCleanupDispatchCleanup 回调例程,请使用DRIVER_DISPATCH类型,如以下代码示例所示:

DRIVER_DISPATCH MyDispatchCleanup;

然后,按如下所示实现回调例程:

_Use_decl_annotations_
NTSTATUS
  MyDispatchCleanup(
    struct _DEVICE_OBJECT  *DeviceObject,
    struct _IRP  *Irp
    )
  {
      // Function body
  }

DRIVER_DISPATCH函数类型在 Wdm.h 头文件中定义。 若要在运行代码分析工具时更准确地识别错误,请务必将the_Use_decl_annotations_annotation添加到函数定义中。 The_Use_decl_annotations_annotation可确保使用应用于头文件中DRIVER_DISPATCH函数类型的注释。 有关函数声明要求的详细信息,请参阅 使用 WDM 驱动程序的函数角色类型来声明函数。 有关 Use_decl_annotations的信息,请参阅 批注函数行为

要求

要求 价值
目标平台 桌面
标头 wdm.h (包括 Wdm.h、Ntddk.h、Ntifs.h)
IRQL 在PASSIVE_LEVEL调用(请参阅“备注”部分)。