Windows Driver Development Fundamentals (v) Data structures for drivers

Source: Internet
Author: User

Windows Driver Development Fundamentals: The data structure of the driver. Reprint please indicate source:http://blog.csdn.net/ikerpeng/article/details/38794405


It is important that the I/O manager defines some data structures.


1. Drive Object (Driver_object)


Defined by a typedef with a struct:

<span style= "Font-family:microsoft Yahei;" >typedef struct{pdevice_object deviceobject;//point to the device object created by the driver </span>
<span style= "Font-family:microsoft Yahei;" >unicode_string  drivername//driver name punicode_string hardwaredatabase;//Record the hardware database name of the device, which is also recorded in UNICODE string pfast_ The dispatch function used in the Io_dispatch fastiodispatch;//file driver pdriver_initialize driverinit;//points to the DriverEntry function, which is established through the IO Manager. Pdriver_startio driverstartio;//records the function address of the Startio routine for serialization operations Pdriver_unload driverunload;//Specifies the address of the callback function used to drive the unload Pdriver_ DISPATCH majorfunction[irp_mj_num+1];//An array of dispatchxxx function pointers pointing to the driver}driver_object,*pdriver_object; Array of dispatchxxx function pointers to the driver </span>

There are some important data structures in it:


DeviceObject: The device object, which may be one or more. Created by the program ape itself, when the driver is unloaded, traverse every driverobject and delete;

DeviceName: Device name, stored in Unicode string

Hardwaredatabase:


Hardwaredatabase records the hardware database name of the device, which is also recorded in a Unicode string. The string is typically Hkey_local_machine\hardware\description\system and is a registry path.
Fastiodispatch:


The dispatch function used in the file driver. A structure that points to the FastIO entry point definition for this driver. This member can only be used by FSDS and network transport drivers.
Driverinit:


Point to the DriverEntry function, which is established through the IO Manager.
Driverstartio:


Record the function address of the Startio routine for serialization operations, and if a driver does not have a startio function, this member will be null.
Driverunload:


Specifies the address of the callback function used when the driver unloads, and if the driver does not have an unload function, this member will be null.
MAJORFUNCTION[IRP_MJ_NUM+1]:


An array of dispatchxxx function pointers to the driver. Each driver must set at least one DISPATCHXXX function pointer in this array to handle this driver IRP request packet. Any one driver can set as many dispatchxxx as irp_mj_xxx code to handle the IRP request package. Each DISPATCHXXX structure is as follows:
NTSTATUS dispatchxxx (in Pdevice_object deviceobjec, in Pirp Irp);


2. Device Object (DeviceObject):


A picture of the relationship of the members inside:


Each driver will have a device object, and each device object has a pointer to the next device object (the last one is empty). The structure is defined as follows:

<span style= "Font-family:microsoft Yahei;"    >typedef struct declspec_align (memory_allocation_alignment) _device_object{CSHORT Type;    USHORT Size;    LONG Referencecount;    /* Pointer to drive object in driver */struct _driver_object *driverobject;    /* Pointer to next device object */struct _device_object *nextdevice;    struct _device_object *attacheddevice;    /* Current IRP structure */struct _IRP *currentirp;    Pio_timer TIMER;    /* Attribute flags for device objects */ULONG flags;    ULONG characteristics;    _volatile PVPB VPB;    /* Pointer to Device extension object */PVOID deviceextension;    /* Specify device Type */Device_type DeviceType;    /* The minimum number of layers for the stack */CChar StackSize;        Union {list_entry ListEntry;    Wait_context_block WCB;    } Queue;    /* Memory Alignment */ULONG alignmentrequirement;    Kdevice_queue Devicequeue;    KDPC Dpc;    /* * The following members are used to support mutex operations on file systems * To keep track of the count of devices used by file system processing Threads */ULONG Activethreadcount;    Psecurity_descriptor SecurityDescriptor;     KEVENT Devicelock;    USHORT sectorsize;     USHORT Spare1; struct _devobj_extensioN *deviceobjectextension; PVOID Reserved; } device_object;typedef struct _device_object *pdevice_object;</span>

the specific meanings of the driver accessible members in the device object are described below:


Pdriver_object DriverObject: Points to the driver object in the driver. The driver object pointer of the same generic driver points to the same drive object.

Pdevice_object Nextdevice: Points to the next device object. The next device object here is one of several device objects created by the same driver. Each device object is based on the

Nextdevice members form a linked list, which iterates through each device object. After each successful call to IoCreateDevice, the I/O manager updates the linked list. When the driver is unloaded, it is necessary to traverse the list and delete each device object.


PIRP CURRENTIRP: If the driver uses Startio routines, this member will point to the current IRP structure, otherwise null.


ULONG Flags: This member is a 32-bit unsigned integer variable, with each bit having a different meaning. You can set different attributes for newly created device objects by using the bitwise OR operation.


ULONG Characteristics: This member describes the properties of the device object, and when the driver calls Iocreate-device, it can set the File_removable_media (indicating that the storage device supports removable media), File_read_ Only_device (indicates that the device cannot be written), File_floppy_diskette (indicates that the device is a floppy device) is equivalent.


PVOID deviceextension: Points to the device extension object.


Device_type DeviceType: Specify the type of device, set by IoCreateDevice, and fill in the appropriate device type as required by the device.


CChar StackSize: In the case of a multilayer drive, a stack-like structure is formed between the drive and the drive. The IRP is passed from the highest level to the bottom in turn. StackSize is the minimum number of layers used to specify the stack position of the IRP sent to the drive. IoCreateDevice sets the member in a newly created device object.


ULONG AlignmentRequirement: The address alignment of the canonical device when transmitting data


The DeviceType: Indicates the type of device that is selected when acting as a virtual device: File_device_unknown.



3. Device Extension:


Device objects record generic information, and some special information is recorded in the device extension.

It is best not to use global variables in the driver, which often leads to the non-reentrant nature of the function. This problem can be solved by storing global variables as device extensions and protecting them .


Reference documents:


A detailed description of the Windows Driver Development technology

Http://www.cnblogs.com/qintangtao/archive/2013/04/09/3009790.html

Windows Driver Development Fundamentals (v) Data structures for drivers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.