驱动对象(DRIVER_OBJECT)

驱动对象(DRIVER_OBJECT)

 每个驱动程序对象代表了一个加载了的内核模式驱动程序映像.这个驱动对象就是以
DRIVER_OBJECT结构体的形式存在的.这个驱动对象的指针从驱动程序的DriverEntry函数
或AddDevice函数的参数传入的.

typedef struct
{
    PDEVICE_OBJECT DeviceObject;     // 设备对象链中的第一个
  PUNICODE_STRING HardwareDatabase;
  PFAST_IO_DISPATCH FastIoDispatch;
  PDRIVER_INITIALIZE DriverInit;
  PDRIVER_STARTIO DriverStartIo;
  PDRIVER_UNLOAD DriverUnload;
  PDRIVER_DISPATCH MajorFunction[IRP_MJ_NUM+1];
}DRIVER_OBJECT,*PDRIVER_OBJECT;

结构体成员:
DeviceObject:
  指向驱动程序创建的设备对象.这个驱动程序调用IoCreateDevice的时候会自动赋予正确的设备对象指针.

HardwareDatabase:
  指向一个字符串.这个字符串是一个注册表路径,这个注册表路径位于的HKEY_LOCAL_MACHINE\Hardware.

FastIoDispatch:
  指向这个驱动程序的FastIO入口点定义的一个结构.这个成员只能通过FSDs和网络传输驱动来使用.

DriverInit
  指向DriverEntry函数的,这是通过IO管理器来建立的.

DriverStartIo
  指向驱动程序的StartIo函数,这是在驱动程序初始化的时候通过DriverEntry来设置的.
如果一个驱动程序没有StartIo函数,这个成员将是NULL.

DriverUnload
  指向驱动程序卸载函数入口点.在驱动程序初始化的时候通过DriverEntry来设置,如果
驱动程序没有卸载函数,这个成员将是NULL.

MajorFunction[IRP_MJ_NUM+1]
  指向驱动程序的DispatchXXX函数指针的数组.每个驱动程序至少要设置一个DispatchXXX函
数指针在这个数组里来处理这个驱动程序IRP请求包.任何一个驱动程序可以设置和IRP_MJ_XXX代
码一样多的DispatchXXX来处理IRP请求包.每个DispatchXXX结构如下:
  NTSTATUS DispatchXXX(IN PDEVICE_OBJECT DeviceObjec, IN PIRP Irp);

头文件:
  这个结构定义在wdm.h和ntddk.h里面.应该包含wdm.h或ntddk.h

说明:
  每个内核模式驱动程序初始化函数的名字应该是DriverEntry,所以系统将自动加载驱动程序的
入口函数.如果入口函数的名字是别的话,这个驱动程序的开发者必须在链接时定义初始化函数的名
字;否则操作系统或Io管理器不能定位驱动程序入口地址.

  一个驱动程序必须设置它的DispatchXXX入口地址在这个驱动对象里,换句话说,就是在驱动加载
的时候传给驱动对象的MajorFunction成员.一个设备驱动程序必须设置一个或多个DispatchXXX入口
地址在MajorFunction成员里,使得IRP_MJ_XXX类型的IRP请求包可以给驱动程序处理.

 驱动程序处理完IRP之后应该传给下一个驱动程序.至于更多的关于IRP_MJ_XXX的设置及类型,请
看MSDN的IRP Function Codes and IOCTLS.

  DriverEntry函数也设置驱动程序的StartIo函数和卸载函数的入口点在驱动对象里.

  HardwareDatabase字符串能在驱动程序加载的时候从注册表的得到硬件信息.这个字符串是只读的.
  从DriverEntry的参数里输入的注册表路径指向
HKEY_LOCAL_MACHINE\system\CurrentControlSet\Services\”驱动程序的名字编码”.这个字符串是只读的.


typedef struct _DRIVER_OBJECT {
    CSHORT Type; // 结构类型
    CSHORT Size; // 结构大小

    //
    // The following links all of the devices created by a single driver
    // together on a list, and the Flags word provides an extensible flag
    // location for driver objects.
    //

    PDEVICE_OBJECT DeviceObject;
    ULONG Flags;

    //
    // The following section describes where the driver is loaded.  The count
    // field is used to count the number of times the driver has had its
    // registered reinitialization routine invoked.
    //

    PVOID DriverStart;
    ULONG DriverSize;
    PVOID DriverSection;
    PDRIVER_EXTENSION DriverExtension;

    //
    // The driver name field is used by the error log thread
    // determine the name of the driver that an I/O request is/was bound.
    //

    UNICODE_STRING DriverName;

    //
    // The following section is for registry support.  Thise is a pointer
    // to the path to the hardware information in the registry
    //

    PUNICODE_STRING HardwareDatabase;

    //
    // The following section contains the optional pointer to an array of
    // alternate entry points to a driver for "fast I/O" support.  Fast I/O
    // is performed by invoking the driver routine directly with separate
    // parameters, rather than using the standard IRP call mechanism.  Note
    // that these functions may only be used for synchronous I/O, and when
    // the file is cached.
    //

    PFAST_IO_DISPATCH FastIoDispatch;

    //
    // The following section describes the entry points to this particular
    // driver.  Note that the major function dispatch table must be the last
    // field in the object so that it remains extensible.
    //

    PDRIVER_INITIALIZE DriverInit;
    PDRIVER_STARTIO DriverStartIo;
    PDRIVER_UNLOAD DriverUnload;
    PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1];

} DRIVER_OBJECT;
typedef struct _DRIVER_OBJECT *PDRIVER_OBJECT;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值