现在的位置: 首页 > 综合 > 正文

[Windows驱动开发](三)Windows驱动开发常用的数据结构

2013年07月18日 ⁄ 综合 ⁄ 共 4268字 ⁄ 字号 评论关闭
  •     本节主要介绍驱动开发的一些基础知识。

转自:

http://blog.csdn.net/baggiowangyu/article/details/7804567

    1. 驱动程序的基本组成

        1.1. 最经常见到的数据结构

               a. DRIVER_OBJECT驱动对象

  1. // WDK中对驱动对象的定义  
  2. // 每个驱动程序都会有一个唯一的驱动对象与之对应  
  3. // 它是在驱动加载时被内核对象管理程序创建的  
  4.   
  5. typedef struct _DRIVER_OBJECT {  
  6.     CSHORT Type;  
  7.     CSHORT Size;  
  8.   
  9.     //  
  10.     // The following links all of the devices created by a single driver  
  11.     // together on a list, and the Flags word provides an extensible flag  
  12.     // location for driver objects.  
  13.     //  
  14.   
  15.     PDEVICE_OBJECT DeviceObject;  
  16.     ULONG Flags;  
  17.   
  18.     //  
  19.     // The following section describes where the driver is loaded.  The count  
  20.     // field is used to count the number of times the driver has had its  
  21.     // registered reinitialization routine invoked.  
  22.     //  
  23.   
  24.     PVOID DriverStart;  
  25.     ULONG DriverSize;  
  26.     PVOID DriverSection;  
  27.     PDRIVER_EXTENSION DriverExtension;  
  28.   
  29.     //  
  30.     // The driver name field is used by the error log thread  
  31.     // determine the name of the driver that an I/O request is/was bound.  
  32.     //  
  33.   
  34.     UNICODE_STRING DriverName;  
  35.   
  36.     //  
  37.     // The following section is for registry support.  Thise is a pointer  
  38.     // to the path to the hardware information in the registry  
  39.     //  
  40.   
  41.     PUNICODE_STRING HardwareDatabase;  
  42.   
  43.     //  
  44.     // The following section contains the optional pointer to an array of  
  45.     // alternate entry points to a driver for "fast I/O" support.  Fast I/O  
  46.     // is performed by invoking the driver routine directly with separate  
  47.     // parameters, rather than using the standard IRP call mechanism.  Note  
  48.     // that these functions may only be used for synchronous I/O, and when  
  49.     // the file is cached.  
  50.     //  
  51.   
  52.     PFAST_IO_DISPATCH FastIoDispatch;  
  53.   
  54.     //  
  55.     // The following section describes the entry points to this particular  
  56.     // driver.  Note that the major function dispatch table must be the last  
  57.     // field in the object so that it remains extensible.  
  58.     //  
  59.   
  60.     PDRIVER_INITIALIZE DriverInit;  
  61.     PDRIVER_STARTIO DriverStartIo;  
  62.     PDRIVER_UNLOAD DriverUnload;  
  63.     PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1];  
  64.   
  65. } DRIVER_OBJECT;  
  66. typedef struct _DRIVER_OBJECT *PDRIVER_OBJECT;   



参数说明:

  • DeviceObject : 每个驱动程序都会有至少一个设备对象。每个设备对象都有一个指向下一个设备对象的指针,最后一个设备对象指向空。此参数指的是驱动对象的第一个设备对象。设备对象的创建与删除都是由程序员自行处理的。
  • DriverName : 驱动名称,由UNICODE_STRING记录。一般格式为\Driver\[DriverName]
  • HardwareDatabase :  设备的硬件数据库名称。一般格式为\REGISTRY\MACHINE\HARDWARE\DESCRIPTION\SYSTEM
  • DriverStartIo : 记录StartIO派发函数地址,用于序列化操作。
  • DriverUnload : 指定驱动卸载时的回调函数地址。
  • MajorFunction : 记录处理IRP的派发函数的函数地址。
  • FastIoDispatch : 文件驱动中会用到此成员,用于处理快速IO请求。

驱动对象图解:

b. DEVICE_OBJECT设备对象

  1. // WDK定义的设备对象  
  2.   
  3. typedef struct DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT) _DEVICE_OBJECT {  
  4.     CSHORT Type;  
  5.     USHORT Size;  
  6.     LONG ReferenceCount;  
  7.     struct _DRIVER_OBJECT *DriverObject;  
  8.     struct _DEVICE_OBJECT *NextDevice;  
  9.     struct _DEVICE_OBJECT *AttachedDevice;  
  10.     struct _IRP *CurrentIrp;  
  11.     PIO_TIMER Timer;  
  12.     ULONG Flags;                                // See above:  DO_...  
  13.     ULONG Characteristics;                      // See ntioapi:  FILE_...  
  14.     __volatile PVPB Vpb;  
  15.     PVOID DeviceExtension;  
  16.     DEVICE_TYPE DeviceType;  
  17.     CCHAR StackSize;  
  18.     union {  
  19.         LIST_ENTRY ListEntry;  
  20.         WAIT_CONTEXT_BLOCK Wcb;  
  21.     } Queue;  
  22.     ULONG AlignmentRequirement;  
  23.     KDEVICE_QUEUE DeviceQueue;  
  24.     KDPC Dpc;  
  25.   
  26.     //  
  27.     //  The following field is for exclusive use by the filesystem to keep  
  28.     //  track of the number of Fsp threads currently using the device  
  29.     //  
  30.   
  31.     ULONG ActiveThreadCount;  
  32.     PSECURITY_DESCRIPTOR SecurityDescriptor;  
  33.     KEVENT DeviceLock;  
  34.   
  35.     USHORT SectorSize;  
  36.     USHORT Spare1;  
  37.   
  38.     struct _DEVOBJ_EXTENSION  *DeviceObjectExtension;  
  39.     PVOID  Reserved;  
  40.   
  41. } DEVICE_OBJECT;  
  42.   
  43. typedef struct _DEVICE_OBJECT *PDEVICE_OBJECT;   



参数说明:

  • DriverObject : 指向驱动程序中的驱动对象。如果多个设备对象属于同一个驱动程序,则它们所指的驱动对象是相同的。
  • NextDevice : 指向下一个设备对象。
  • AttachedDevice : 指向下一个设备对象。如果有更高一层的驱动附加到这个驱动的时候,其指向的就是更高一层的那个驱动。
  • CurrentIrp : 使用StartIO派发函数的时候,它指向的是当前的IRP结构
  • Flags : 标志域,32位无符号整形,其值有以下几种:
    1. DO_BUFFERED_IO : 读写操作使用缓冲方式(系统复制缓冲区)访问用户模式数据。
    2. DO_EXCLUSIVE : 一次只允许一个线程打开设备句柄。
    3. DO_DIRECT_IO : 读写操作使用直接方式(内存描述表)访问用户模式数据。
    4. DO_DEVICE_INITIALIZING : 设备对象正在初始化。
    5. DO_P

抱歉!评论已关闭.