学习驱动开发一段时间了,按照我学习的知识写了一个简单的串口过滤程序,可是不知为啥,加载驱动后计算机就重启,实在是搞不清哪个地方出了问题,望高手指点。下面是代码。
#define arraysize(p) (sizeof(p)/sizeof((p)[0]))
static PDEVICE_OBJECT pDevTopObj;
NTSTATUS ReadIRP(PDEVICE_OBJECT pDevObj,PIRP pIRP)
{
long i;
PIO_STACK_LOCATION irpsp=IoGetCurrentIrpStackLocation(pIRP);
ULONG len=irpsp->Parameters.Write.Length;
PUCHAR buf=NULL;
if (pIRP->MdlAddress!=NULL)
buf=(PUCHAR)MmGetSystemAddressForMdlSafe(pIRP->MdlAddress,NormalPagePriority);
else
buf=(PUCHAR)pIRP->UserBuffer;
for ( i=0;i<len;i++)
{
KdPrint(("%c",buf[i]));
}
IoSkipCurrentIrpStackLocation(pIRP);
return IoCallDriver(pDevTopObj,pIRP);
}
NTSTATUS DispatchAny(PDEVICE_OBJECT pDevObj,PIRP pIRP)
{
PIO_STACK_LOCATION irpsp = IoGetCurrentIrpStackLocation(pIRP);
if(irpsp->MajorFunction == IRP_MJ_POWER)
{
// 直接发送,然后返回说已经被处理了。
PoStartNextPowerIrp(pIRP);
IoSkipCurrentIrpStackLocation(pIRP);
return PoCallDriver(pDevTopObj,pIRP);
}
IoSkipCurrentIrpStackLocation(pIRP);
return IoCallDriver(pDevTopObj,pIRP);
}
VOID UnLoad(PDRIVER_OBJECT pDriObj)
{
DbgBreakPoint();
}
#pragma code_seg("INIT")
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriObj,UNICODE_STRING strRegPath)
{
NTSTATUS status;
PDEVICE_OBJECT devObj;
UNICODE_STRING devName;
int i;
DbgBreakPoint();
//创建过滤设备对象
RtlInitUnicodeString(&devName,L"\\Device\\Serial0");
for (i=0;i<arraysize(pDriObj->MajorFunction);i++)
{
pDriObj->MajorFunction[i]=DispatchAny;
}
pDriObj->MajorFunction[IRP_MJ_READ]=ReadIRP;
pDriObj->DriverUnload=UnLoad;//0x2a
status=IoCreateDevice(pDriObj,0,NULL,FILE_DEVICE_BUS_EXTENDER,0,FALSE,&devObj);
if (status==STATUS_SUCCESS)
{
status=IoAttachDevice(devObj,&devName,&pDevTopObj);
if (status==STATUS_SUCCESS)
{
if(pDevTopObj->Flags & DO_BUFFERED_IO)
devObj->Flags |= DO_BUFFERED_IO;
if(pDevTopObj->Flags & DO_DIRECT_IO)
devObj->Flags |= DO_DIRECT_IO;
if(pDevTopObj->Flags & DO_BUFFERED_IO)
devObj->Flags |= DO_BUFFERED_IO;
if(pDevTopObj->Characteristics & FILE_DEVICE_SECURE_OPEN)
devObj->Characteristics |= FILE_DEVICE_SECURE_OPEN;
devObj->Flags |= DO_POWER_PAGABLE;
devObj->Flags = devObj->Flags & ~DO_DEVICE_INITIALIZING;
}
}
return status;
}
你的串口读的地不能这么写,要指写读的完成例程回调,因为IO还没有完成读。