DispatchDeviceControl routine

The DispatchDeviceControl routine services IRPs containing the IRP_MJ_DEVICE_CONTROL I/O function code.


DRIVER_DISPATCH DispatchDeviceControl;

NTSTATUS DispatchDeviceControl(
  _Inout_ struct _DEVICE_OBJECT *DeviceObject,
  _Inout_ struct _IRP           *Irp
{ ... }


DeviceObject [in, out]

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.

Irp [in, out]

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.


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].

Input parameters for all DispatchXxx 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. The I/O stack location contains the I/O control code specifying the device control operation to be performed.

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.

Generally, all DispatchXxx 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 DispatchDeviceControl routines, see Writing Dispatch Routines. For more information about IRPs, see Handling IRPs.


To define a DispatchDeviceControl 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 DispatchDeviceControl callback routine that is named MyDispatchDeviceControl, use the DRIVER_DISPATCH type as shown in this code example:

DRIVER_DISPATCH MyDispatchDeviceControl;

Then, implement your callback routine as follows:

    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.


Target platform



Wdm.h (include Wdm.h, Ntddk.h, or Ntifs.h)


Called at PASSIVE_LEVEL (see Remarks section).



Send comments about this topic to Microsoft