HidD_GetAttributes如何从设备对象中检索属性?

时间:2016-03-11 09:37:24

标签: c windows driver hid wdf

我正致力于在Windows 10中创建虚拟HID设备。为了准备自己编写驱动程序,我一直在分析此处提供的示例:https://github.com/Microsoft/Windows-driver-samples/tree/master/hid/vhidmini2

在app / testvhid.c文件中,HidD_GetAttributes用于检索HID设备的属性。似乎属性在driver / vhidmini.c中初始化(EvtDeviceAdd函数中的hidAttributes)。但是,hidAttributes存储在DEVICE_CONTEXT结构中,(根据我的理解)由驱动程序定义。

如果hidAttributes是用户定义的属性,那么HidD_GetAttributes如何知道从设备中检索它?

我试图通过创建一个主要基于给定样本的KMDF驱动程序来为自己复制这个。相关的驱动程序代码如下(其中大部分来自示例,但调用WdfDeviceCreateDeviceInterface除外):

NTSTATUS
kmdfTest2CreateDevice(
    _Inout_ PWDFDEVICE_INIT DeviceInit
    )
{
    WDF_OBJECT_ATTRIBUTES   deviceAttributes;
    PDEVICE_CONTEXT deviceContext;
    WDFDEVICE device;
    NTSTATUS status;
    PHID_DEVICE_ATTRIBUTES  hidAttributes;

    WdfFdoInitSetFilter(DeviceInit);

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&deviceAttributes, DEVICE_CONTEXT);

    status = WdfDeviceCreate(&DeviceInit, &deviceAttributes, &device);

    if (NT_SUCCESS(status)) {
        deviceContext = DeviceGetContext(device);
        deviceContext->Device = device;
        deviceContext->DeviceData = 0;

        hidAttributes = &deviceContext->HidDeviceAttributes;
        RtlZeroMemory(hidAttributes, sizeof(HID_DEVICE_ATTRIBUTES));
        hidAttributes->Size = sizeof(HID_DEVICE_ATTRIBUTES);
        hidAttributes->VendorID = HIDMINI_VID;
        hidAttributes->ProductID = HIDMINI_PID;
        hidAttributes->VersionNumber = HIDMINI_VERSION;

        if (NT_SUCCESS(status)) {
            status = kmdfTest2QueueInitialize(device);
        }

        deviceContext->HidDescriptor = G_DefaultHidDescriptor;
        deviceContext->ReportDescriptor = G_DefaultReportDescriptor;

        status = WdfDeviceCreateDeviceInterface(
            device,
            &GUID_DEVINTERFACE_HID,
            NULL // ReferenceString
            );
    }

    return status;
}

用户模式测试应用程序中相关的代码部分如下(此代码几乎完全来自给定的示例):

BOOLEAN
CheckIfOurDevice(
    HANDLE file)
{
    PHIDP_PREPARSED_DATA Ppd; // The opaque parser info describing this device
    HIDP_CAPS Caps; // The Capabilities of this hid device.
    HIDD_ATTRIBUTES attr; // Device attributes

    if (!HidD_GetAttributes(file, &attr))
    {
        printf("Error: HidD_GetAttributes failed \n");
        return FALSE;
    }

    printf("Device Attributes - PID: 0x%x, VID: 0x%x \n", attr.ProductID, attr.VendorID);
    if ((attr.VendorID != HIDMINI_VID) || (attr.ProductID != HIDMINI_PID))
    {
        printf("Device attributes doesn't match the sample \n");
        return FALSE;
    }

    if (!HidD_GetPreparsedData(file, &Ppd))
    {
        printf("Error: HidD_GetPreparsedData failed \n");
        return FALSE;
    }

    if (!HidP_GetCaps(Ppd, &Caps))
    {
        printf("Error: HidP_GetCaps failed \n");
        HidD_FreePreparsedData(Ppd);
        return FALSE;
    }

    if ((Caps.UsagePage == g_MyUsagePage) && (Caps.Usage == g_MyUsage)) {
        printf("Success: Found my device.. \n");
        return TRUE;
    }
    else {
        printf("failed: UsagePage: %d, Usage: %d \n", Caps.UsagePage, Caps.Usage);
    }

    return FALSE;

}

BOOLEAN
GetHidInterface(_Out_ HANDLE* Handle) {
    CONFIGRET cr = CR_SUCCESS;
    *Handle = INVALID_HANDLE_VALUE;
    ULONG deviceInterfaceListLength = 0;
    GUID InterfaceGuid;
    PSTR deviceInterfaceList = NULL;
    HANDLE devHandle = INVALID_HANDLE_VALUE;

    if (NULL == Handle) {
        printf("Error: Invalid device handle parameter\n");
        return FALSE;
    }

    HidD_GetHidGuid(&InterfaceGuid);

    cr = CM_Get_Device_Interface_List_Size(
        &deviceInterfaceListLength,
        &InterfaceGuid,
        NULL,
        CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
    if (cr != CR_SUCCESS) {
        printf("Error 0x%x retrieving device interface list size.\n", cr);
        return FALSE;
    }
    if (deviceInterfaceListLength <= 1) {
        printf("Error: No active device interfaces found.\n"
            " Is the sample driver loaded?");
        return FALSE;
    }
    deviceInterfaceList = (PSTR)malloc(deviceInterfaceListLength * sizeof(WCHAR));
    if (deviceInterfaceList == NULL) {
        printf("Error allocating memory for device interface list.\n");
        return FALSE;
    }
    ZeroMemory(deviceInterfaceList, deviceInterfaceListLength * sizeof(WCHAR));
    cr = CM_Get_Device_Interface_List(
        &InterfaceGuid,
        NULL,
        deviceInterfaceList,
        deviceInterfaceListLength,
        CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
    if (cr != CR_SUCCESS) {
        printf("Error 0x%x retrieving device interface list.\n", cr);
        return FALSE;
    }

    printf("\n....looking for our HID device (with UP=0x%04X "
        "and Usage=0x%02X)\n", g_MyUsagePage, g_MyUsage);

    PSTR currentInterface;
    for (currentInterface = deviceInterfaceList;
    *currentInterface;
        currentInterface += strlen(currentInterface) + 1) {

        devHandle = CreateFile(currentInterface,
            GENERIC_READ | GENERIC_WRITE,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            NULL, // no SECURITY_ATTRIBUTES structure
            OPEN_EXISTING, // No special create flags
            0, // No special attributes
            NULL); // No template file

        if (INVALID_HANDLE_VALUE == devHandle) {
            printf("Warning: CreateFile failed: %d\n", GetLastError());
            continue;
        }

        if (CheckIfOurDevice(devHandle)) {          
            *Handle = devHandle;
            return TRUE;
        }
        else {
            CloseHandle(devHandle);
        }
    }
    return FALSE;
}

HidD_GetAttributes函数通过,但CheckIfOurDevice函数在检查设备属性时失败(&#34;设备属性与样本&#34不匹配;)。我已经检查了HidD_GetAttributes设置的一些属性值,但它们与驱动程序设置的属性值不匹配。样品是否正在拨打我失踪的其他电话?

0 个答案:

没有答案