NTSTATUS
IoCreateDevice(
IN PDRIVER_OBJECT DriverObject,--本驱动对象
IN ULONG DeviceExtensionSize,--这里不使用,为NULL即可
IN PUNICODE_STRING DeviceName OPTIONAL,--可选的名字,过滤设备不要名称
IN DEVICE_TYPE DeviceType,--和要绑定的设备类型一致
IN ULONG DeviceCharacteristics,--书上凭经验设为O
IN BOOLEAN Exclusive,--保留为系统使用,直接定位false即可
OUT PDEVICE_OBJECT *DeviceObject--OUT宏指定为输出参数,即返回值,返回本过滤设备指针
);
IoCreateDevice:下面一段英文解释
IoCreateDevice creates a device object and returns a pointer to the object. The caller is responsible
for deleting the object when it is no longer needed by calling IoDeleteDevice.
IoCreateDevice can only be used to create an unnamed device object, or a named device object for which
a security descriptor is set by an INF file. Otherwise, drivers must use IoCreateDeviceSecure to
create
named device objects. For more information, see Creating a Device Object. The caller is responsible
for
setting certain members of the returned device object. For more information, see Initializing a Device
Object and the device-type-specific documentation for your device.
Be careful to specify the DeviceType and DeviceCharacteristics values in the correct parameters. Both
parameters use system-defined FILE_XXX constants and some driver writers specify the values in the
wrong
parameters by mistake.
Device objects for disks, tapes, CD-ROMs, and RAM disks are given a Volume Parameter Block (VPB) that
is
initialized to indicate that the volume has never been mounted on the device.
If a driver's call to IoCreateDevice returns an error, the driver should release any resources that it
allocated for that device.
Callers of IoCreateDevice must be running at IRQL <= APC_LEVEL.
捡主要的讲,意思是该函数创建一个设备对象并返回这个对象的指针,当你不再需要这个设备时请调IoDeleteDevice
去删除它,这样我们又学到了这个函数。而且IoCreateDevice只能创建没有名字的设备,若想创建命名的设备,则使
用函数IoCreateDeviceSecure。另外这里提到是小心使用这两个参数,DeviceType,DeviceCharacteristics,他们
都是被定义成FILE_XXX 这样的结构,如果该函数调用失败,那么请释放之前所有用到的资源。最后这个函数运行在
APC_LEVEL级别以下。
一切设置好以后,就可以开始绑定了,
IoAttachDeviceToDeviceStack(*fltobj,oldobj); returns a pointer to the device object to which the
SourceDevice
was attached。返回一个被过滤设备绑定后的设备指针,其实这里有点疑问?这个返回的不就是要被绑定的设备么
,这个设
备现在存在于最上层了,等待我后面验证。***********验证**********
默认情况下,绑定设备之后设备并没有启动,书上给出这么一句,
(*fltobj)->Flags = (*fltobj)->Flags & ~DO_DEVICE_INITIALIZING;
我在DDK帮助上找到这样一句,
Function and filter drivers must clear the DO_DEVICE_INITIALIZING flag. 这样就可以理解了,清除相应的
标志位就可以了,意思是不需要初始化,直接启动即可,因为他和要绑定的设备是同样的。
指针都没有
你叫人家啥啊,书上说是打开串口,可我觉得整个过程根本就没有没碰到打开串口这个东西啊,我把它叫做获得串口
设备对象指针函数吧。假设这个设备有名字的话,可以用函数IoGetDeviceObjectPionter来得到对象指针。
DDK中有这样的评论:
This routine also returns a pointer to the corresponding file object. When unloading, a driver can
dereference
the file object as a means of indirectly dereferencing the device object. To do so, the driver calls
ObDereferenceObject
from its Unload routine, passing the file object pointer returned by IoGetDeviceObjectPointer.
这个例程同样返回一个文件对象指针,当卸载的时候这个驱动必须解除这个文件对象指针的引用。不管怎么样,一定
记得解除引用。一般地,串口在在设备中总是以固定形式的名字出现的,如\device\serial0,\device\serial1之类
的,那么可变的就是一个序号而已。在此引入内核中的一个格式化字符的函数,同sprintf有异曲同工之处。
即RtlStringCchPrintf,参数没什么好解释的,看DDK上微软的一个范例就OK了,
DDK上原版说明的,请看
IoCreateDevice can only be used to create an unnamed device object, or a named device object for which a security descriptor is set by an INF file. Otherwise, drivers must use IoCreateDeviceSecure to create named device objects. For more information, see Creating a Device Object. The caller is responsible for setting certain members of the returned device object. For more information, see Initializing a Device Object and the device-type-specific documentation for your device.