驱动代码很短,就是创建了一个设备对象,然后挂在\Device\KeyboardClass0设备上,然后在IRP_MJ_READ例程中设置了一个完成例程,可是驱动运行之后,一按键盘就蓝屏,我看了以下DUMP文件,说是Probably caused by : kbdclass.sys ( kbdclass!KeyboardClassServiceCallback+77 ),肯定是我的程序的原因啊,系统怎么会出错呢?
请帮忙分析一下蓝屏原因:
#include <ntddk.h>
#include <ntddkbd.h>
UNICODE_STRING SymbolLinkName;
PDEVICE_OBJECT pKeyboardFilterObj,pAttchedObj;
//----------------完成例程---------------------------------------------
NTSTATUS IoCompletion(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp,IN PVOID Context)
{
PKEYBOARD_INPUT_DATA KeyDataBuffer;
int NumberOfKeys,i;
if (NT_SUCCESS(Irp->IoStatus.Status))
{
KeyDataBuffer=Irp->AssociatedIrp.SystemBuffer;
NumberOfKeys=Irp->IoStatus.Information/sizeof(PKEYBOARD_INPUT_DATA);
for (i=0;i<NumberOfKeys;i++)
{
DbgPrint("%x\n",KeyDataBuffer[i].MakeCode);
}
}
if (Irp->PendingReturned)
{
IoMarkIrpPending(Irp);
}
return Irp->IoStatus.Status;
}
//-----------此例程负责向下转发所有的自己不想处理的IRP-------------
NTSTATUS GeneralRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp)
{
IoCopyCurrentIrpStackLocationToNext(pIrp);
return IoCallDriver(pAttchedObj,pIrp);
}
//----------过滤IRP_MJ_READ读请求---------------------------------
NTSTATUS Read(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp)
{
IoCopyCurrentIrpStackLocationToNext(pIrp);
IoSetCompletionRoutine(pIrp,(PIO_COMPLETION_ROUTINE)IoCompletion,NULL,TRUE,TRUE,TRUE);
return IoCallDriver(pAttchedObj,pIrp);
}
VOID Unload(IN PDRIVER_OBJECT pDriverObject)
{
IoDeleteSymbolicLink(&SymbolLinkName);
IoDeleteDevice(pKeyboardFilterObj);
}
NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDrvObj,IN PUNICODE_STRING pregstring)
{
UNICODE_STRING DevName;
UNICODE_STRING SymbolLinkName;
UNICODE_STRING KeyboardObjName;
NTSTATUS status;
int i;
do
{
for (i=0;i<IRP_MJ_MAXIMUM_FUNCTION;i++)
{
pDrvObj->MajorFunction[i]=GeneralRoutine; //把所有的IRP都发到下层
}
pDrvObj->MajorFunction[IRP_MJ_READ]=Read;
pDrvObj->DriverUnload=Unload;
RtlInitUnicodeString(&DevName,L"\\Device\\KeyboardFilter");
status=IoCreateDevice(pDrvObj,0,&DevName,FILE_DEVICE_UNKNOWN,0,FALSE,&pKeyboardFilterObj);
if(!NT_SUCCESS(status))
{
break;
}
pKeyboardFilterObj->Flags|=DO_DIRECT_IO;
pKeyboardFilterObj->Flags&=~DO_DEVICE_INITIALIZING;
RtlInitUnicodeString(&SymbolLinkName,L"\\??\\KeyboardFilter");
IoCreateSymbolicLink(&SymbolLinkName,&DevName);
//将自身挂在键盘设备上
RtlInitUnicodeString(&KeyboardObjName,L"\\Device\\KeyboardClass0");
status=IoAttachDevice(pKeyboardFilterObj,&KeyboardObjName,&pAttchedObj);
if(!NT_SUCCESS(status))
{
break;
}
return status;
} while (FALSE);
if (pKeyboardFilterObj!=NULL)
{
IoDeleteDevice(pKeyboardFilterObj);
}
return STATUS_UNSUCCESSFUL;
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!