我编写了一个简单的键盘过滤器驱动程序(MyKbdFilter),在注册表中注册并添加到HLM \ SYSTEM \ CurrentControlSet \ Control \ Class {4D36E96B-E325-11CE-BFC1-08002BE10318} \ UpperFilters MyKbdFilter。现在我有:" kbdclass MyKbdFilter"。因此,当为键盘构建新的设备堆栈时,我得到MyKbdFilter-> kdbclass-> kbHid-> HidUsb。当我插入键盘时,我看到调用了DriverEntry,AddDevice,DispatchThru和DispatchPNP。但键盘没有响应。在键击时,既不调用DispatchRead也不调用其他函数。 这是我的代码:
NTSTATUS DriverEntry(IN DRIVER_OBJECT* pDriverObject, IN UNICODE_STRING* pRegPath) {
int i;
NTSTATUS status;
for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++) {
pDriverObject->MajorFunction[i] = DispatchThru;
}
pDriverObject->MajorFunction[IRP_MJ_READ] = DispatchRead;
pDriverObject->MajorFunction[IRP_MJ_POWER] = DispatchPower;
pDriverObject->MajorFunction[IRP_MJ_PNP] = DispatchPNP;
pDriverObject->DriverUnload = UnloadDriver;
pDriverObject->DriverExtension->AddDevice = AddDevice;
return STATUS_SUCCESS;
}
NTSTATUS AddDevice(IN PDRIVER_OBJECT pDriverObject, IN PDEVICE_OBJECT pTargetDeviceObject){
PDEVICE_OBJECT pSourceDeviceObject, pDeviceObject;
PDEVICE_EXTENSION pDeviceExtension;
NTSTATUS status;
status = IoCreateDevice(pDriverObject, sizeof(DEVICE_EXTENSION), NULL, FILE_DEVICE_KEYBOARD, 0, FALSE, &pSourceDeviceObject);
pDeviceExtension = (PDEVICE_EXTENSION) pSourceDeviceObject->DeviceExtension;
pDeviceObject = IoAttachDeviceToDeviceStack(pSourceDeviceObject, pTargetDeviceObject);
pDeviceExtension->pLowerDeviceObject = pTargetDeviceObject;
pSourceDeviceObject->Flags = pDeviceObject->Flags & (DO_BUFFERED_IO | DO_POWER_PAGABLE | DO_DIRECT_IO);
pSourceDeviceObject->Flags = pDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
return status;
}
NTSTATUS DispatchThru(IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIrp) {
IoSkipCurrentIrpStackLocation(pIrp);
return IoCallDriver(((PDEVICE_EXTENSION) pDeviceObject->DeviceExtension)->pLowerDeviceObject ,pIrp);
}
NTSTATUS DispatchRead(IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIrp) {
IoSkipCurrentIrpStackLocation(pIrp);
return IoCallDriver(((PDEVICE_EXTENSION) pDeviceObject->DeviceExtension)->pLowerDeviceObject ,pIrp);
}
编辑:我使用的是Windows 7。
答案 0 :(得分:1)
pDeviceObject = IoAttachDeviceToDeviceStack(pSourceDeviceObject, pTargetDeviceObject);
pDeviceExtension->pLowerDeviceObject = pTargetDeviceObject;
这是错误:
需要感觉pDeviceExtension->pLowerDeviceObject = pDeviceObject;
但真正正确的代码
IoAttachDeviceToDeviceStackSafe(pSourceDeviceObject, pTargetDeviceObject, &pDeviceExtension->pLowerDeviceObject);
这是主要观点,只是可见。
pSourceDeviceObject->Flags = pDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
其他一个错误 - 需要:
pSourceDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;