关于驱动程序的问题
刚刚接触到驱动程序,需要写一个键盘过滤的驱动,遇到一些问题,希望各位大侠帮忙解答一下.
驱动程序启动之后,附加到所有的键盘设备之上,然后,监测键盘输入
如果,键盘被拔出,再插入,这个时候,就监测不到输入了.
我添加了AddDevice例程,希望当键盘插入的时候,可以在这里面,重新附加,但是,这个例程却一直都没有
响应过.但是,拔出的时候,可以收到一个PNP的IRP,在里面,可以处理拔出操作
1.AddDevice需要满足什么条件才能响应?
2.在我写的驱动中,要实时监测键盘的插拔,能实现吗?
下面是一部分代码
NTSTATUS DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
{
ULONG i;
NTSTATUS status;
KdPrint (("c2p.SYS: entering DriverEntry\n"));
// 填写所有的分发函数的指针
for (i = 0; i
{
DriverObject->MajorFunction[i] = c2pDispatchGeneral;
}
DriverObject->DriverExtension->AddDevice = MyAddDevice;
// 单独的填写一个Read分发函数。因为要的过滤就是读取来的按键信息
// 其他的都不重要。这个分发函数单独写。
DriverObject->MajorFunction[IRP_MJ_READ] = c2pDispatchRead;
// 单独的填写一个IRP_MJ_POWER函数。这是因为这类请求中间要调用
// 一个PoCallDriver和一个PoStartNextPowerIrp,比较特殊。
DriverObject->MajorFunction [IRP_MJ_POWER] = c2pPower;
// 我们想知道什么时候一个我们绑定过的设备被卸载了(比如从机器上
// 被拔掉了?)所以专门写一个PNP(即插即用)分发函数
DriverObject->MajorFunction [IRP_MJ_PNP] = c2pPnP;
// 卸载函数。
DriverObject->DriverUnload = c2pUnload;
gDriverObject = DriverObject;
// 绑定所有键盘设备f
status = c2pAttachDevices(DriverObject, RegistryPath);
return status;
}
DRIVER_ADD_DEVICE MyAddDevice;
NTSTATUS MyAddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pPhysicalDevice)
{
KdPrint(("In MYAddDevice"));
NTSTATUSstatus= STATUS_SUCCESS;
PDEVICE_OBJECT pFilterDevice= NULL;
PDEVICE_OBJECTpLowerDevice= NULL;
// 创建过滤设备
/*IoCreateDevice(
IN DriverObject,
IN sizeof(C2P_DEV_EXT),
IN NULL,
IN pTargetDeviceObject->DeviceType,
IN pTargetDeviceObject->Characteristics,
IN FALSE,
OUT &pFilterDeviceObject
);*/
status = IoCreateDevice(DriverObject,sizeof(C2P_DEV_EXT),NULL,
pPhysicalDevice->DeviceType, pPhysicalDevice->Characteristics, FALSE, &pFilterDevice);
if(!NT_SUCCESS(status))
{
KdPrint(("-----Fail ==> KeyFilter::CY_KeyFilte::AddDevice->IoCreateDevice(%d)",status));
return status;
}
C2P_DEV_EXT *pDevExt = (C2P_DEV_EXT *)pFilterDevice->DeviceExtension;
pDevExt->NodeSize = sizeof(C2P_DEV_EXT);
pDevExt->pFilterDeviceObject = pFilterDevice;
pLowerDevice = IoAttachDeviceToDeviceStack(pFilterDevice, pPhysicalDevice);// 绑定
if(!pLowerDevice)
{
KdPrint(("-----Fail ==> KeyFilter::CY_KeyFilte::AddDevice->IoAttachDeviceToDeviceStack(%d)",status));
return STATUS_UNSUCCESSFUL;
}
pFilterDevice->Flags |= pLowerDevice->Flags & (DO_BUFFERED_IO | DO_DIRECT_IO | DO_POWER_PAGABLE) ;
pFilterDevice->Flags &= ~DO_DEVICE_INITIALIZING;
pDevExt->LowerDeviceObject = pLowerDevice;
pDevExt->TargetDeviceObject = pPhysicalDevice;
return status;
}
NTSTATUS c2pPnP(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
)
{
KdPrint(("PNP in\n"));
PC2P_DEV_EXT devExt;
PIO_STACK_LOCATION irpStack;