阅读:1502回复:1
为什么我在函数中创建符号不能在R3中使用呢?
代码如下:
#include "MyFileSystem.h" #include <ntdddisk.h> /************************************************************************ * 函数名称:DriverEntry * 功能描述:初始化驱动程序,定位和申请硬件资源,创建内核对象 * 参数列表: pDriverObject:从I/O管理器中传进来的驱动对象 pRegistryPath:驱动程序在注册表的中的路径 * 返回 值:返回初始化驱动状态 *************************************************************************/ int g_Sum = 0; #pragma INITCODE extern "C" NTSTATUS DriverEntry ( IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath ) { NTSTATUS status; KdPrint(("Enter DriverEntry\n")); //注册其他驱动调用函数入口 pDriverObject->DriverUnload = MyUnload; pDriverObject->MajorFunction[IRP_MJ_CREATE] = MyCreateFileRoutine; pDriverObject->MajorFunction[IRP_MJ_CLOSE] = MyCloseRoutine; pDriverObject->MajorFunction[IRP_MJ_WRITE] = MyWriteFileRoutine; pDriverObject->MajorFunction[IRP_MJ_READ] = MyReadFileRoutine; pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = MyControlRoutine; //创建驱动设备对象 status = CreateDevice(pDriverObject); KdPrint(("DriverEntry end\n")); return status; } /************************************************************************ * 函数名称:CreateDevice * 功能描述:初始化设备对象 * 参数列表: pDriverObject:从I/O管理器中传进来的驱动对象 * 返回 值:返回初始化状态 *************************************************************************/ #pragma INITCODE NTSTATUS CreateDevice ( IN PDRIVER_OBJECT pDriverObject) { NTSTATUS status; PDEVICE_OBJECT pDevObj; PDEVICE_EXTENSION pDevExt; //创建设备名称 UNICODE_STRING devName; RtlInitUnicodeString(&devName,L"\\Device\\MyDevice"); //创建设备 status = IoCreateDevice( pDriverObject, sizeof(DEVICE_EXTENSION), &(UNICODE_STRING)devName, FILE_DEVICE_DISK, 0, TRUE, &pDevObj ); if (!NT_SUCCESS(status)) return status; pDevObj->Vpb->Size = 3; pDevObj->Flags = DO_DIRECT_IO ; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->pDevice = pDevObj; pDevExt->ustrDeviceName = devName; //创建符号链接 UNICODE_STRING symLinkName; RtlInitUnicodeString(&symLinkName,L"\\??\\MySymbolName"); pDevExt->ustrSymLinkName = symLinkName; status = IoCreateSymbolicLink( &symLinkName,&devName ); if (!NT_SUCCESS(status)) { IoDeleteDevice(pDevObj); return status; } //IoRegisterFileSystem(pDevExt->pDevice); return STATUS_SUCCESS; } /************************************************************************ * 函数名称:MyUnload * 功能描述:负责驱动程序的卸载操作 * 参数列表: pDriverObject:驱动对象 * 返回 值:返回状态 *************************************************************************/ #pragma PAGEDCODE VOID MyUnload (IN PDRIVER_OBJECT pDriverObject) { PDEVICE_OBJECT pNextObj; KdPrint(("Enter DriverUnload\n")); pNextObj = pDriverObject->DeviceObject; while (pNextObj != NULL) { PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION) pNextObj->DeviceExtension; //删除符号链接 UNICODE_STRING pLinkName = pDevExt->ustrSymLinkName; IoDeleteSymbolicLink(&pLinkName); pNextObj = pNextObj->NextDevice; //IoUnregisterFileSystem(pDevExt->pDevice); IoDeleteDevice( pDevExt->pDevice ); } } /************************************************************************ * 函数名称:MyDispatchRoutine * 功能描述:对读IRP进行处理 * 参数列表: pDevObj:功能设备对象 pIrp:从IO请求包 * 返回 值:返回状态 *************************************************************************/ #pragma PAGEDCODE NTSTATUS MyDispatchRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) { KdPrint(("Enter DispatchRoutine\n")); NTSTATUS status = STATUS_SUCCESS; // 完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; // bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint(("Leave DispatchRoutine\n")); return status; } #pragma PAGEDCODE NTSTATUS MyCreateFileRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) { KdPrint(("Enter CreateFile\n")); NTSTATUS status = STATUS_SUCCESS; // 完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = FILE_OPENED ; // bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint(("Leave CreateFile\n")); return status; } #pragma PAGEDCODE NTSTATUS MyReadFileRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) { KdPrint(("Enter ReadFile\n")); NTSTATUS status = STATUS_SUCCESS; // 完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; // bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint(("Leave ReadFile\n")); return status; } #pragma PAGEDCODE NTSTATUS MyWriteFileRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) { KdPrint(("Enter WriteFile\n")); NTSTATUS status = STATUS_SUCCESS; // 完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; // bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint(("Leave WriteFile\n")); return status; } NTSTATUS MyCloseRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) { KdPrint(("Enter Close\n")); NTSTATUS status = STATUS_SUCCESS; // 完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; // bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint(("Leave Close\n")); return status; } NTSTATUS MyControlRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp) { KdPrint(("Enter Control\n")); NTSTATUS status = STATUS_SUCCESS; if (g_Sum ==0) { MyCreateDevice(pDevObj->DriverObject); } // 完成IRP pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; // bytes xfered IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint(("Leave Control\n")); return status; } VOID MyCreateDevice(IN PDRIVER_OBJECT pDriverObject) { ; NTSTATUS status; PDEVICE_OBJECT pDevObj; PDEVICE_EXTENSION pDevExt; //创建设备名称 UNICODE_STRING devName; RtlInitUnicodeString(&devName,L"\\Device\\MyDevice1"); //创建设备 status = IoCreateDevice(pDriverObject, sizeof(DEVICE_EXTENSION), &(UNICODE_STRING)devName, FILE_DEVICE_DISK, 0, TRUE, &pDevObj); if (!NT_SUCCESS(status)) return ; pDevObj->Flags = DO_DIRECT_IO ; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->pDevice = pDevObj; pDevExt->ustrDeviceName = devName; //创建符号链接 UNICODE_STRING symLinkName; RtlInitUnicodeString(&symLinkName,L"\\??\\MySymbolName1"); pDevExt->ustrSymLinkName = symLinkName; status = IoCreateSymbolicLink( &symLinkName,&devName ); //这里不会报错的 if (!NT_SUCCESS(status)) { IoDeleteDevice(pDevObj); return ; } g_Sum++; //IoRegisterFileSystem(pDevExt->pDevice); } 应用层用CreateFile"\\\\.\\MySymbolName"打开下一个设备就会创建, 这时再用CreateFile"\\\\.\\MySymbolName1"就提示路径错误 winobj中也不会有MySymbolName1 只有 一个MySymbolName DeviceTree中看设备是对的, |
|
沙发#
发布于:2010-02-26 11:52
知道了,结贴
|
|