2016-10-20 108 views
0

我寫了一個簡單的鍵盤過濾驅動程序(MyKbdFilter),其註冊註冊表,並添加到HLM \系統\ CurrentControlSet \控制\ {類4D36E96B-E325-11CE-BFC1-08002BE10318} \的UpperFilters MyKbdFilter。現在我有:「kbdclass MyKbdFilter」。所以,當爲鍵盤構建一個新的設備堆棧時,我會得到MyKbdFilter-> kdbclass-> kbHid-> HidUsb。當我插入鍵盤時,我看到DriverEntry,AddDevice,DispatchThru和DispatchPNP被調用。但鍵盤沒有響應。在關鍵筆劃上既不調用DispatchRead也不調用其他函數。 這裏是我的代碼:鍵盤過濾驅動程序不responsing

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

回答

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; 
+0

謝謝!我不知道爲什麼我會設置較低的設備) – Michael

相關問題