阅读:3601回复:3
真是太奇怪了,总是蓝屏,很简单的入门代码,请大家帮看看
请大家帮看看,谢谢了,困扰了两天了,总是蓝屏,真是不知道哪里错了?
#include "Driver.h" #define PAGECODE code_seg("PAGE") #define LOCKEDCODE code_seg() #define INITCODE code_seg("INIT") #define PAGEDDATA data_seg("PAGE") #define LOCKEDDATA data_seg() #define INITDATA data_seg("INIT") #define arraysize(p) (sizeof(p)/sizeof((p)[0])) #define MAX_FILE_LENGTH 1024 typedef struct _DEVICE_EXTENSION { PDEVICE_OBJECT pDevice; UNICODE_STRING ustrDeviceName; UNICODE_STRING ustrSymLinkName; ULONG file_length; UCHAR* buffer; } DEVICE_EXTENSION, *PDEVICE_EXTENSION; NTSTATUS CreateDevice( IN PDRIVER_OBJECT pDriverObject ); VOID HelloDDKUnload( IN PDRIVER_OBJECT pDriverObject ); NTSTATUS HelloDDKDispatchRoutin( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ); NTSTATUS HelloDDKReadForBuffered( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ); NTSTATUS HelloDDKWriteForBuffered( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ); NTSTATUS HelloDDKQueryInformation( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ); #pragma INITCODE extern "C" NTSTATUS DriverEntry( IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegisterPath ) { NTSTATUS status; KdPrint(("Enter DriverEntry\n")); pDriverObject->DriverUnload = HelloDDKUnload; for (int i = 0; i < arraysize(pDriverObject->MajorFunction); ++i) pDriverObject->MajorFunction = HelloDDKDispatchRoutin; pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloDDKWriteForBuffered;//HelloDDKDispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_READ] = HelloDDKReadForBuffered;//HelloDDKDispatchRoutine; pDriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = HelloDDKQueryInformation; status = CreateDevice( pDriverObject ); KdPrint(("DriverEntry end\n")); return status; } #pragma INITCODE NTSTATUS CreateDevice( IN PDRIVER_OBJECT pDriverObject ) { NTSTATUS status; PDEVICE_OBJECT pDevObj; PDEVICE_EXTENSION pDevExt; UNICODE_STRING devName; RtlInitUnicodeString( &devName, L"\\Device\\MyDDKDevice" ); status = IoCreateDevice( pDriverObject, sizeof( DEVICE_EXTENSION ), &devName, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevObj ); if( !NT_SUCCESS( status ) ) return status; pDevObj->Flags |= DO_BUFFERED_IO; pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; pDevExt->pDevice = pDevObj; pDevExt->ustrDeviceName = devName; pDevExt->file_length = 0;//MAX_FILE_LENGTH; pDevExt->buffer = (UCHAR *)ExAllocatePool( PagedPool, MAX_FILE_LENGTH ); RtlFillMemory( (PVOID)pDevExt->buffer, MAX_FILE_LENGTH, 0 ); // Create symbol link UNICODE_STRING symLinkName; RtlInitUnicodeString( &symLinkName, L"\\??\\HelloDDK" ); pDevExt->ustrSymLinkName = symLinkName; status = IoCreateSymbolicLink( &symLinkName, &devName ); if( !NT_SUCCESS( status ) ) { IoDeleteDevice( pDevObj ); return status; } return STATUS_SUCCESS; } #pragma PAGEDCODE VOID HelloDDKUnload( IN PDRIVER_OBJECT pDriverObject ) { PDEVICE_OBJECT pNextDev; KdPrint( ("Enter DriverUnload \n") ); pNextDev = pDriverObject->DeviceObject; while( pNextDev != NULL ) { PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pNextDev->DeviceExtension; if( pDevExt->buffer != NULL ) { pDevExt->file_length = 0; ExFreePool( pDevExt->buffer ); pDevExt->buffer = NULL; } UNICODE_STRING pLinkName = pDevExt->ustrSymLinkName; IoDeleteSymbolicLink( &pLinkName ); pNextDev = pNextDev->NextDevice; IoDeleteDevice( pDevExt->pDevice ); } } #pragma PAGEDCODE VOID CheckIrpType( PIO_STACK_LOCATION irp_stack ); NTSTATUS HelloDDKDispatchRoutin( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { KdPrint( ("Enter HelloDDKDispatchRoutine \n") ); PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation( pIrp ); CheckIrpType( stack ); NTSTATUS status = STATUS_SUCCESS; pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint( ("Leave HelloDDKDispatchRoutine \n") ); return status; } VOID CheckIrpType( PIO_STACK_LOCATION irp_stack ) { KdPrint( ("Enter CheckIrpType()\n") ); static char* irpname[] = { "IRP_MJ_CREATE", "IRP_MJ_CREATE_NAMED_PIPE", "IRP_MJ_CLOSE", "IRP_MJ_READ", "IRP_MJ_WRITE", "IRP_MJ_QUERY_INFORMATION", "IRP_MJ_SET_INFORMATION", "IRP_MJ_QUERY_EA", "IRP_MJ_SET_EA", "IRP_MJ_FLUSH_BUFFERS", "IRP_MJ_QUERY_VOLUMNE_INFORMATION", "IRP_MJ_SET_VOLUME_INFORMATION", "IRP_MJ_DIRECTORY_CONTROL", "IRP_MJ_FILE_SYSTEM_CONTROL", "IRP_MJ_DEVICE_CONTROL", "IRP_MJ_INTERNAL_DEVICE_CONTROL", "IRP_MJ_SHUTDOWN", "IRP_MJ_LOCK_CONTROL", "IRP_MJ_CLEANUP", "IRP_MJ_CREATE_MAILSLOT", "IRP_MJ_QUERY_SECURITY", "IRP_MJ_SET_SECURITY", "IRP_MJ_POWER", "IRP_MJ_SYSTEM_CONTROL", "IRP_MJ_DEVICE_CHANGE", "IRP_MJ_QUERY_QUOTA", "IRP_MJ_SET_QUOTA", "IRP_MJ_PNP", }; UCHAR type = irp_stack->MajorFunction; if (type >= arraysize(irpname) ) KdPrint( (" - Unknown IRP, major type %X\n", type) ); else KdPrint( ("\t%s\n", irpname[type]) ); KdPrint( ("Leave CheckIrpType()\n") ); } NTSTATUS HelloDDKReadForBuffered( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { KdPrint( ("Enter HelloDDKReadForBuffered\n") ); PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; NTSTATUS status = STATUS_SUCCESS; PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation( pIrp ); ULONG read_length = stack->Parameters.Read.Length; ULONG read_offset = (ULONG)stack->Parameters.Read.ByteOffset.QuadPart; if (read_length + read_offset > MAX_FILE_LENGTH) { status = STATUS_FILE_INVALID; read_length = 0; } else { memcpy( pIrp->AssociatedIrp.SystemBuffer, pDevExt->buffer + read_offset, read_length ); status = STATUS_SUCCESS; } pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = read_length; // memset( pIrp->AssociatedIrp.SystemBuffer, 0xAA, read_length ); IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint( ("Leave HelloDDKReadForBuffered\n") ); return status; } NTSTATUS HelloDDKClose( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { KdPrint( ("Enter HelloDDKClose\n") ); NTSTATUS status = STATUS_SUCCESS; PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; //pDevExt-> pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = 0; IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint( ("Leave HelloDDKClose\n") ); return status; } NTSTATUS HelloDDKWriteForBuffered( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { KdPrint( ("Enter HelloDDKWriteForBuffered\n") ); NTSTATUS status = STATUS_SUCCESS; PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation( pIrp ); ULONG write_length = stack->Parameters.Write.Length; ULONG write_offset = (ULONG)stack->Parameters.Write.ByteOffset.QuadPart; if ( write_offset + write_length > MAX_FILE_LENGTH ) { status = STATUS_FILE_INVALID; write_length = 0; } else { memcpy( pDevExt->buffer + write_offset, pIrp->AssociatedIrp.SystemBuffer, write_length ); status = STATUS_SUCCESS; if ( write_length + write_offset > pDevExt->file_length ) { pDevExt->file_length = write_length + write_offset; } } //KdPrint( ("Leave HelloDDKWriteForBuffered\n") ); pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = write_length; IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint( ("Leave HelloDDKWriteForBuffered\n") ); return status; } NTSTATUS HelloDDKQueryInformation( IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp ) { KdPrint( ("Enter HelloDDKQueryInformation\n") ); #if DBG // __asm int 3 #endif PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation( pIrp ); PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension; FILE_INFORMATION_CLASS info = stack->Parameters.QueryFile.FileInformationClass; if ( FileStandardInformation == info ) { KdPrint(("FileStandardInformation\n")); PFILE_STANDARD_INFORMATION file_info = (PFILE_STANDARD_INFORMATION)pIrp->AssociatedIrp.SystemBuffer; file_info->EndOfFile = RtlConvertLongToLargeInteger(pDevExt->file_length); } NTSTATUS status = STATUS_SUCCESS; pIrp->IoStatus.Status = status; pIrp->IoStatus.Information = stack->Parameters.QueryFile.Length; IoCompleteRequest( pIrp, IO_NO_INCREMENT ); KdPrint(("Leave HelloDDKQueryInformation\n")); return status; } |
|
沙发#
发布于:2009-08-04 23:43
不好意思,找到问题原因了,宏用错了应该是 #pragma PAGECODE, 不是#pragma PAGEDCODE.
真是惭愧! |
|
板凳#
发布于:2012-03-12 14:19
Faint
|
|
地板#
发布于:2012-03-23 13:18
学习了,以后写代码要认真仔细。
|
|