MSDN Documentation

Device Objects

Device objects are fundamental structures in Windows kernel-mode driver development. They represent a physical or logical device and are used by the operating system and drivers to interact with the hardware. Each device object is managed by the I/O Manager and is associated with a specific driver.

The Role of Device Objects

A device object serves as an interface between the operating system's I/O subsystem and the driver responsible for managing a device. When an application or another driver requests an operation on a device, the I/O Manager creates an I/O Request Packet (IRP) and sends it to the appropriate driver's dispatch routine, often through a device object.

Device Object Structure

The primary structure for a device object is DEVICE_OBJECT, defined in the wdm.h header file. This structure contains numerous fields that hold information about the device, including:

// Simplified representation of DEVICE_OBJECT structure
            typedef struct _DEVICE_OBJECT {
                CSHORT                       Type;
                CSHORT                       Size;
                // ... other fields ...
                PDEVICE_EXTENSION            DeviceExtension;
                struct _DEVICE_OBJECT        *NextDevice;
                USHORT                       StackSize;
                // ... more fields ...
            } DEVICE_OBJECT, *PDEVICE_OBJECT;

Creating and Managing Device Objects

Device objects are typically created by a driver's AddDevice routine. This routine is called by the Plug and Play (PnP) manager when a new instance of a device represented by the driver is detected. The driver then allocates memory for the device object and its device extension, initializes them, and attaches the device object to the device tree.

Tip: The device extension is crucial for storing device-specific data, such as hardware registers, buffer pointers, and state information. Its size is specified when creating the device object using IoCreateDevice.

Types of Device Objects

Device Object Attachment

Device objects are linked together to form a device tree. When a driver creates a device object, it typically attaches it to the device tree below the device object that represented its parent (e.g., a function driver attaches its FDO below the PDO created by the bus driver).

Important: Proper management of device object lifetimes is critical. Drivers must ensure that device objects are deleted when the device is removed or the driver is unloaded to prevent system instability. This is usually handled in the driver's PnP Dispatch routine.

Further Reading