阅读:922回复:0
求助!一个简单驱动出现了问题(附带源码)
小弟在学习驱动,最近按照书,写了一个简单的驱动程序,不需要物理设备,是一个LoopBack程序(仿照那本2000驱动指南写的)
但是,在用DRIVERMONITOR 调试的时候,一运行"Start driver" 马上重启,郁闷中,希望高手解答。 源码如下 Driver.c #include "Driver.h" VOID DriverUnload(IN PDRIVER_OBJECT pDriverObject) { PDEVICE_OBJECT pDevObj; UNICODE_STRING pLinkName; pDevObj=pDriverObject->DeviceObject; if(pDevObj!=NULL) { PDEVICE_EXTENSION pDevExt=(PDEVICE_EXTENSION)pDevObj->DeviceExtension; pLinkName=pDevExt->DeviceWin32Name; if (pDevExt->DeviceBuffer!=NULL) { ExFreePool(pDevExt->DeviceBuffer); pDevExt->DeviceBuffer=NULL; pDevExt->DeviceBufferSize=0; } IoDeleteSymbolicLink(&pLinkName); IoDeleteDevice(pDevExt->pDeviceObject); } } NTSTATUS DispatchCreate(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) { pIrp->IoStatus.Status=STATUS_SUCCESS; pIrp->IoStatus.Information=0; IoCompleteRequest(pIrp,IO_NO_INCREMENT); return STATUS_SUCCESS; } NTSTATUS DispatchClose(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { PDEVICE_EXTENSION pDevExt; pDevExt=(PDEVICE_EXTENSION)pDevObj->DeviceExtension; if (pDevExt->DeviceBuffer!=NULL) { ExFreePool(pDevExt->DeviceBuffer); pDevExt->DeviceBuffer=NULL; pDevExt->DeviceBufferSize=0; } pIrp->IoStatus.Status=STATUS_SUCCESS; pIrp->IoStatus.Information=0; IoCompleteRequest(pIrp,IO_NO_INCREMENT); return STATUS_SUCCESS; } NTSTATUS DispatchRead(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) { NTSTATUS status = STATUS_SUCCESS; PDEVICE_EXTENSION pDevExt; PVOID UserBuffer; ULONG XferSize; PIO_STACK_LOCATION pIrpStack; pIrpStack = IoGetCurrentIrpStackLocation(pIrp); UserBuffer = pIrp->AssociatedIrp.SystemBuffer; XferSize = pIrpStack->Parameters.Read.Length; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; XferSize = (XferSize < pDevExt->DeviceBufferSize) ? XferSize:pDevExt->DeviceBufferSize; RtlCopyMemory(UserBuffer,pDevExt->DeviceBuffer,XferSize); ExFreePool(pDevExt->DeviceBuffer); pDevExt->DeviceBuffer = NULL; pDevExt->DeviceBufferSize = 0; pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = XferSize; IoCompleteRequest(pIrp,IO_NO_INCREMENT); return status; } NTSTATUS DispatchWrite(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) { NTSTATUS status = STATUS_SUCCESS; PDEVICE_EXTENSION pDevExt; PVOID UserBuffer; ULONG XferSize; PIO_STACK_LOCATION pIrpStack; pIrpStack = IoGetCurrentIrpStackLocation(pIrp); UserBuffer = pIrp->AssociatedIrp.SystemBuffer; XferSize = pIrpStack->Parameters.Write.Length; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; if (pDevExt->DeviceBuffer != NULL) { ExFreePool(pDevExt->DeviceBuffer); pDevExt->DeviceBuffer = NULL; XferSize = 0; } pDevExt->DeviceBuffer = ExAllocatePool(PagedPool,XferSize); if (pDevExt->DeviceBuffer == NULL) { status = STATUS_INSUFFICIENT_RESOURCES; XferSize = 0; } else { pDevExt->DeviceBufferSize = XferSize; RtlCopyMemory(pDevExt->DeviceBuffer,UserBuffer,XferSize); } pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = XferSize; IoCompleteRequest(pIrp,IO_NO_INCREMENT); return status; } NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegPath ) { NTSTATUS status; UNICODE_STRING NtName; UNICODE_STRING Win32Name; PDEVICE_OBJECT pDevObj; PDEVICE_EXTENSION pDevExt; pDriverObject->DriverUnload=DriverUnload; pDriverObject->MajorFunction [IRP_MJ_CREATE] = DispatchCreate; pDriverObject->MajorFunction [IRP_MJ_CLOSE] = DispatchClose; pDriverObject->MajorFunction [IRP_MJ_READ] = DispatchRead; pDriverObject->MajorFunction [IRP_MJ_WRITE] = DispatchWrite; RtlInitUnicodeString(&NtName,DeviceInterName); RtlInitUnicodeString(&Win32Name,DeviceLinkName); status = IoCreateDevice( pDriverObject, sizeof(DEVICE_EXTENSION), &NtName, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevObj ); if (status != STATUS_SUCCESS) { IoDeleteDevice(pDevObj); return status; } RtlZeroMemory(pDevObj->DeviceExtension,sizeof(DEVICE_EXTENSION)); pDevObj->Flags |= DO_BUFFERED_IO; pDevExt=(PDEVICE_EXTENSION)(pDevObj)->DeviceExtension; pDevExt->pDeviceObject = pDevObj; pDevExt->DeviceNTName=NtName; pDevExt->DeviceBuffer = NULL; pDevExt->DeviceBufferSize = 0; pDevExt->DeviceWin32Name=Win32Name; status=IoCreateSymbolicLink(&Win32Name,&NtName); return status; } Driver.h #include "D:\NTDDK\inc\ddk\ntddk.h" #include "stdlib.h" #define DeviceInterName L"\\Device\\LoopBack" #define DeviceLinkName L"\\??\\LoopBack" typedef struct _DEVICE_EXTENSION { PDEVICE_OBJECT pDeviceObject; PVOID DeviceBuffer; ULONG DeviceBufferSize; UNICODE_STRING DeviceNTName; UNICODE_STRING DeviceWin32Name; }DEVICE_EXTENSION,*PDEVICE_EXTENSION; |
|