DRIVER_DISPATCH callback function (wdm.h)

The callback routine services various IRPs. For a list of function codes, see Remarks.

Syntax

DRIVER_DISPATCH DriverDispatch;

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

Parameters

[in, out] DeviceObject

Caller-supplied pointer to a DEVICE_OBJECT structure. This is the device object for the target device, previously created by the driver's AddDevice routine.

[in, out] Irp

Caller-supplied pointer to an IRP structure that describes the requested I/O operation.

Return value

If the routine succeeds, it must return STATUS_SUCCESS. Otherwise, it must return one of the error status values defined in Ntstatus.h.

Remarks

Input parameters for all Dispatch routines are supplied in the IRP structure pointed to by Irp. Additional parameters are supplied in the driver's associated I/O stack location, which is described by the IO_STACK_LOCATION structure and can be obtained by calling IoGetCurrentIrpStackLocation.

Generally, all Dispatch routines execute in an arbitrary thread context at IRQL = PASSIVE_LEVEL, but there are exceptions. For more information, see Dispatch Routines and IRQLs.

For more information about dispatch routines, see Writing Dispatch Routines. For more information about IRPs, see Handling IRPs.

IRP About implementing the callback
IRP_MJ_CLEANUP A driver's DispatchCleanup routine should be named XxxDispatchCleanup, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchCleanup routine's address in DriverObject->MajorFunction[IRP_MJ_CLEANUP].
IRP_MJ_CLOSE A driver's DispatchClose routine should be named XxxDispatchClose, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchClose routine's address in DriverObject->MajorFunction[IRP_MJ_CLOSE].
IRP_MJ_CREATE A driver's DispatchCreate routine should be named XxxDispatchCreate, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchCreate routine's address in DriverObject->MajorFunction[IRP_MJ_CREATE].
IRP_MJ_CREATE or IRP_MJ_CLOSE A driver can provide a single DispatchCreateClose routine instead of separate DispatchCreate and DispatchClose routines.

A driver's DispatchCreateClose routine should be named XxxDispatchCreateClose, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchCreateClose routine's address in DriverObject->MajorFunction[IRP_MJ_CREATE] and in DriverObject->MajorFunction[IRP_MJ_CLOSE].
IRP_MJ_DEVICE_CONTROL A driver's DispatchDeviceControl routine should be named XxxDispatchDeviceControl, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchDeviceControl routine's address in DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL].

The system uses the FILE_XXX flags in the I/O control code to determine whether the IRP sender has the privileges to send the IRP to the device object. Drivers for Windows Server 2003 and later versions of Windows can use the IoValidateDeviceIoControlAccess routine to perform stricter access checks within DispatchDeviceControl.
IRP_MJ_FLUSH_BUFFERS A driver's DispatchFlushBuffers routine should be named XxxDispatchFlushBuffers, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchFlushBuffers routine's address in DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS].
IRP_MJ_INTERNAL_DEVICE_CONTROL A driver's DispatchInternalDeviceControl routine should be named XxxDispatchInternalDeviceControl, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchInternalDeviceControl routine's address in DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL].
IRP_MJ_PNP A driver's DispatchPnP routine should be named XxxDispatchPnP, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchPnP routine's address in DriverObject->MajorFunction[IRP_MJ_PNP].
IRP_MJ_POWER A driver's DispatchPower routine should be named XxxDispatchPower, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchPower routine's address in DriverObject->MajorFunction[IRP_MJ_POWER].
IRP_MJ_QUERY_INFORMATION A driver's DispatchQueryInformation routine should be named XxxDispatchQueryInformation, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchQueryInformation routine's address in DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION].
IRP_MJ_READ A driver's DispatchRead routine should be named XxxDispatchRead, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchRead routine's address in DriverObject->MajorFunction[IRP_MJ_READ].
IRP_MJ_READ or IRP_MJ_WRITE A driver can provide a single DispatchReadWrite routine instead of separate DispatchRead and DispatchWrite routines.

A driver's DispatchReadWrite routine should be named XxxDispatchReadWrite, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchReadWrite routine's address in DriverObject->MajorFunction[IRP_MJ_READ] and in DriverObject->MajorFunction[IRP_MJ_WRITE].
IRP_MJ_SET_INFORMATION A driver's DispatchSetInformation routine should be named XxxDispatchSetInformation, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchSetInformation routine's address in DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION].
IRP_MJ_SHUTDOWN A driver's DispatchShutdown routine should be named XxxDispatchShutdown, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchShutdown routine's address in DriverObject->MajorFunction[IRP_MJ_SHUTDOWN].

Additionally, in order to receive IRP_MJ_SHUTDOWN requests, a driver must call IoRegisterShutdownNotification or IoRegisterLastChanceShutdownNotification to register its DispatchShutdown routine with the system.
IRP_MJ_SYSTEM_CONTROL A driver's DispatchSystemControl routine should be named XxxDispatchSystemControl, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchSystemControl routine's address in DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL].
IRP_MJ_WRITE A driver's DispatchWrite routine should be named XxxDispatchWrite, where Xxx is a driver-specific prefix. The driver's DriverEntry routine must store the DispatchWrite routine's address in DriverObject->MajorFunction[IRP_MJ_WRITE].

Examples

To define a callback routine, you must first provide a function declaration that identifies the type of callback routine you're defining. Windows provides a set of callback function types for drivers. Declaring a function using the callback function types helps Code Analysis for Drivers, Static Driver Verifier (SDV), and other verification tools find errors, and it's a requirement for writing drivers for the Windows operating system.

For example, to define a DispatchCleanup callback routine that is named MyDispatchCleanup, use the DRIVER_DISPATCH type as shown in this code example:

DRIVER_DISPATCH MyDispatchCleanup;

Then, implement your callback routine as follows:

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

The DRIVER_DISPATCH function type is defined in the Wdm.h header file. To more accurately identify errors when you run the code analysis tools, be sure to add the_Use_decl_annotations_annotation to your function definition. The_Use_decl_annotations_annotation ensures that the annotations that are applied to the DRIVER_DISPATCH function type in the header file are used. For more information about the requirements for function declarations, see Declaring Functions by Using Function Role Types for WDM Drivers. For information about Use_decl_annotations, see Annotating Function Behavior.

Requirements

Requirement Value
Target Platform Desktop
Header wdm.h (include Wdm.h, Ntddk.h, Ntifs.h)
IRQL Called at PASSIVE_LEVEL (see Remarks section).