阅读:2310回复:3
为什么用ObReferenceObjectByName和MyIoGetDeviceObjectPointer得到的设备指针不一样呢?
高手帮忙看下:
这个是驱动开发计数详解书上的一个MyIoGetDeviceObjectPointer代码: //模拟IoGetDeviceObjectPointer实现 NTSTATUS MyIoGetDeviceObjectPointer( IN PUNICODE_STRING ObjectName, IN ACCESS_MASK DesiredAccess, OUT PFILE_OBJECT *FileObject, OUT PDEVICE_OBJECT *DeviceObject ) { PFILE_OBJECT fileobject; OBJECT_ATTRIBUTES ObjectAttributes; HANDLE fileHandle; IO_STATUS_BLOCK status_block; NTSTATUS status; //设置要打开设备的设备名 InitializeObjectAttributes(&ObjectAttributes,ObjectName,OBJ_KERNEL_HANDLE, (HANDLE)NULL,(PSECURITY_DESCRIPTOR)NULL); //ZwOpenFile打开设备,返回文件句柄,而ZwCreateFile打开设备,返回设备句柄 status=ZwOpenFile(&fileHandle,DesiredAccess,&ObjectAttributes,&status_block, 0,FILE_NON_DIRECTORY_FILE); //判断操作是否正确 if(NT_SUCCESS(status)) { //得到文件对象指针 status=ObReferenceObjectByHandle(fileHandle,0,*IoFileObjectType,KernelMode, (PVOID *)&fileobject,NULL); } //判断操作是否正确 if(NT_SUCCESS(status)) { *FileObject=fileobject; //得到设备对象指针 *DeviceObject=IoGetBaseFileSystemDeviceObject(fileobject); } //关闭设备文件句柄句柄 ZwClose(fileHandle); return status; } 下面是HelloDDKRead的代码 #pragma PAGEDCODE NTSTATUS HelloDDKRead(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) { KdPrint(("DriverB:Enter B HelloDDKRead\n")); NTSTATUS ntStatus = STATUS_SUCCESS; UNICODE_STRING SymbolicName; RtlInitUnicodeString( &SymbolicName, L"\\??\\HelloDDKA" ); PDEVICE_OBJECT DeviceObject=NULL; PFILE_OBJECT FileObject=NULL; //调用ObReferenceObjectByName获得设备指针 ntStatus=ObReferenceObjectByName(&SymbolicName,OBJ_CASE_INSENSITIVE,NULL,FILE_ALL_ACCESS, IoDeviceObjectType,KernelMode,NULL,(PVOID*)&DeviceObject); KdPrint(("DriverB:ntStatus:%x\n",ntStatus)); KdPrint(("DriverB:DeviceObject:%x\n",DeviceObject)); ntStatus = MyIoGetDeviceObjectPointer(&SymbolicName,FILE_ALL_ACCESS,&FileObject,&DeviceObject); KdPrint(("DriverB:ntStatus:%x\n",ntStatus)); KdPrint(("DriverB:DeviceObject:%x\n",DeviceObject)); KdPrint(("DriverB:FileObject:%x\n",FileObject)); //将引用计数减去1,如果此计数减到0了,将会关闭设备,相当于ZwClose ObDereferenceObject(FileObject); ntStatus=STATUS_SUCCESS; // 完成IRP pIrp->IoStatus.Status = ntStatus; pIrp->IoStatus.Information = 0; // bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint(("DriverB:Leave B HelloDDKRead\n")); return ntStatus; } 打印之后发现,ObReferenceObjectByName函数得到的设备指针是exxxxxxx,以e开头的,而MyIoGetDeviceObjectPointer函数得到的设备指针是8xxxxxxx,以8开头的,两个为什么不一样呢?都是打开同一个设备的啊!!!! 大侠帮忙一下啊,困惑了很久了...... |
|
沙发#
发布于:2010-05-18 14:29
为什么没人呢?这么安静.....
|
|
板凳#
发布于:2010-05-19 11:11
太冷清了,看来这个论坛人气不够,换个论坛试试
|
|
地板#
发布于:2010-05-19 19:04
我对天发下毒誓:我要是再会踩这个论坛,我就不是人!
|
|