KMDF降低过滤器驱动程序

时间:2018-01-11 13:20:48

标签: windows kernel driver kmdf

我正在编写一个过滤器驱动程序,并希望将过滤器连接到系统中的多个物理键盘。 如果我将过滤器连接到第一个键盘" \ Device \ KeyboardClass0"它工作正常,但它不适用于其他。 IoAttachDevice因" \ Device \ KeyboardClass1"而失败。与其他班级相同。我附上了3个物理键盘。

status = IoAttachDevice(gkbdDevice,& TargetDevice,&((PDEVICE_EXTENSION)gkbdDevice-> DeviceExtension) - > kbdDevice);

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

使用以下代码,我现在可以打开第二个键盘但是......

    /*
* Form the Device Name and symbolic name
*/
RtlInitUnicodeString(&devNameFlt, L"\\Device\\KeyboardClass0");
RtlInitUnicodeString(&devName, L"\\Device\\MultikeyboardCnt");

status = IoCreateDevice(pDriverObject, sizeof(DEVICE_EXTENSION),
    &devName,
    FILE_DEVICE_KEYBOARD,
    SYNCHRONIZE,
    FALSE,
    &pDevObj);

if (!NT_SUCCESS(status))
{
    DbgPrint("%s, IoCreateDevice failed:0x%0x\n", __FUNCTION__, status);
    return status;
}

HANDLE hFile;
OBJECT_ATTRIBUTES oa = { sizeof(oa), 0, 0, OBJ_CASE_INSENSITIVE };
oa.ObjectName = &devNameFlt;

IO_STATUS_BLOCK iosb;
status = IoCreateFile(&hFile, SYNCHRONIZE, &oa, &iosb, 0, 0, FILE_SHARE_VALID_FLAGS, FILE_OPEN,
    0, 0, 0, CreateFileTypeNone, 0, IO_ATTACH_DEVICE);
if (!NT_SUCCESS(status))
{
    DbgPrint("%s, IoCreateFile failed to call:0x%0x\n", __FUNCTION__, status);
    return status;
}

/* Get File Object */
PFILE_OBJECT LocalFileObject;
status = ObReferenceObjectByHandle(hFile,
                                   0,
                                   *IoFileObjectType,
                                   KernelMode,
                                   (PVOID*)&LocalFileObject,
                                   NULL);

pLBKdev = IoGetRelatedDeviceObject(LocalFileObject);
DbgPrint("%s at IoCreateFile ok\n", __FUNCTION__);
ObReferenceObject(pLBKdev);

/*
* Retrieve device extension pointer from device object
*/
pDevExt = (PDEVICE_EXTENSION)pLBKdev->DeviceExtension;

status = IoAttachDeviceToDeviceStackSafe(pDevObj, pLBKdev, &pDevExt->kbdDevice);
if (status != STATUS_SUCCESS) {
    DbgPrint("IoGetDeviceObjectPointer failed with error = 0x%0x\n", status);
    goto cleanup_failure;
}

pDevObj->Flags |= DO_BUFFERED_IO;
pDevObj->Flags &= ~DO_DEVICE_INITIALIZING;

/*
* Create the symbolic link name, this is not mandatory
* but can be helpful for user mode apps to communicate
*/
status = IoCreateSymbolicLink(&symLinkNameFlt, &devNameFlt);
if (!NT_SUCCESS(status)) {
    // if it fails now, must delete Device object
    DbgPrint("IoCreateSymbolicLink failed with error = 0x%0x\n", status);
    goto cleanup_failure;
}

但是如果我在键盘上键入一个键,则例程DispatchRead会崩溃。

答案 1 :(得分:0)

尝试在DO_BUFFERED_IO和〜DO_DEVICE_INITIALIZING之后执行IoAttachDeviceToDeviceStackSafe()。

pDevObj->Flags |= DO_BUFFERED_IO;
pDevObj->Flags &= ~DO_DEVICE_INITIALIZING;

status = IoAttachDeviceToDeviceStackSafe(pDevObj, pLBKdev, &pDevExt->kbdDevice);
if (status != STATUS_SUCCESS) {
    DbgPrint("IoGetDeviceObjectPointer failed with error = 0x%0x\n", status);
    goto cleanup_failure;
}