我有一个 DEVINST,我需要设备路径 [英] I have a DEVINST, I need the Device Path

查看:42
本文介绍了我有一个 DEVINST,我需要设备路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开作为复合设备一部分的 WinUSB 设备.我使用 cfgmgr32 找到了正确的子设备,并有它的 DEVINST 编号.为了用WinUSB打开它,我需要先调用CreateFile,为此我需要设备路径.

I'm trying to open a WinUSB device that is part of a composite device. I have located the correct child device using cfgmgr32, and have its DEVINST number. In order to open it with WinUSB, I need to first call CreateFile, for which I need the Device Path.

设备路径如下所示:

\\\\?\\usb#vid_9999&pid_0102#3555303335351909000b0#{a5dcbf10-6530-11d2-901f-00c04fb951ed}

如何获取设备路径?

推荐答案

此函数返回以 NULL 结尾的设备路径列表(这是我们从 CM_Get_Device_Interface_List 获得的)

This function returns a list of NULL-terminated Device Paths (that's what we get from CM_Get_Device_Interface_List)

您需要将DEVINST 和想要的接口GUID 传递给它.

You need to pass it the DEVINST, and the wanted interface GUID.

由于 DEVINST 和接口 GUID 均已指定,CM_Get_Device_Interface_List 很可能会为该接口返回单个设备路径,但从技术上讲,您应该准备好获得多个结果.

Since both the DEVINST and interface GUID are specified, it is highly likely that CM_Get_Device_Interface_List will return a single Device Path for that interface, but technically you should be prepared to get more than one result.

我在生产代码中成功使用此函数获取 USB HUB 的设备接口 (GUID_CLASS_USBHUB):我使用生成的设备路径和 CreateFile 并成功打开它.

I used this function successfully in production code for getting the Device Interface of a USB HUB (GUID_CLASS_USBHUB): I used the resulting Device Path with CreateFile and opened it succesfully.

如果函数返回成功(返回码0),调用者负责删除[]返回的列表

It is responsibility of the caller to delete[] the returned list if the function returns successfully (return code 0)

int GetDevInstInterfaces(DEVINST dev, LPGUID interfaceGUID, wchar_t**outIfaces, ULONG* outIfacesLen)
{
    CONFIGRET cres;
    if (!outIfaces)
        return -1;
    if (!outIfacesLen)
        return -2;

    // Get System Device ID
    WCHAR sysDeviceID[256];
    cres = CM_Get_Device_ID(dev, sysDeviceID, sizeof(sysDeviceID) / sizeof(sysDeviceID[0]), 0);
    if (cres != CR_SUCCESS)
        return -11;

    // Get list size
    ULONG ifaceListSize = 0;
    cres = CM_Get_Device_Interface_List_Size(&ifaceListSize, interfaceGUID, sysDeviceID, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
    if (cres != CR_SUCCESS)
        return -12;

    // Allocate memory for the list
    wchar_t* ifaceList = new wchar_t[ifaceListSize];

    // Populate the list
    cres = CM_Get_Device_Interface_List(interfaceGUID, sysDeviceID, ifaceList, ifaceListSize, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
    if (cres != CR_SUCCESS) {
        delete[] ifaceList;
        return -13;
    }

    // Return list
    *outIfaces = ifaceList;
    *outIfacesLen = ifaceListSize;

    return 0;
}

这篇关于我有一个 DEVINST,我需要设备路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆