c++ - 从设备实例路径字符串中获取设备实例 DWORD

标签 c++ windows winapi device-driver device-instance-id

我得到一个像

这样的设备实例路径
L"\\\\?\\USB#VID_0403&PID_6001#6&2cc2d230&0&2#{219d0508-57a8-4ff5-97a1-bd86587c6c7e}"

来自 IWDFRemoteInterfaceInitialize::RetrieveSymbolicLink .
但是对于 CM_Get_Parent我需要让我发疯的设备的 DEVINST/DWORD。
例如我试过

instancePath = L"\\\\?\\USB#VID_0403&PID_6001#6&2cc2d230&0&2#{219d0508-57a8-4ff5-97a1-bd86587c6c7e}";
HDEVINFO hinfo = SetupDiGetClassDevs(NULL, instancePath, NULL, DIGCF_DEVICEINTERFACE | DIGCF_ALLCLASSES);

和其他一些 SetupDi... 巫毒没有成功。非常感谢任何帮助,因为-如前所述-我几个小时以来都无法解决这种疯狂问题,尽管有许多相反的示例(devid->实例路径),但我还没有找到实例路径->DEVINST.

最佳答案

虽然修改路径可能会奏效,但 Windows 文档明确指出您不应该解析设备路径。

不过,您可以使用 CfgMgr32 API 执行此操作,使用 CM_Get_Device_Interface_PropertyW ()DEVPKEY_Device_InstanceId :

#include <cfgmgr32.h>
#include <initguid.h> // needed for devpkey.h to parse properly
#include <devpkey.h>

#include <cassert>
#include <string>
#include <vector>

/*
 *   @brief The following retrieves the Device Instance ID from the main device path.
 *   @param device_path A device path that has the form of the following:
 *                      \\?\usb#vid_[VENDOR_ID]&pid_[PRODUCT_ID]#INSTANCE_ID#{[DEVICE_INTERFACE_GUID]}
 *
 *                      Where the following components are described as:
 *                          1. VENDOR_ID             - The vendor ID of the device.
 *                          2. PRODUCT_ID            - The product ID of the device.
 *                          3. INSTANCE_ID           - The unique ID generated when the device connects to the host.
 *                          4. DEVICE_INTERFACE_GUID - The GUID that describes the interface of the device.
 *                                                     This is NOT the same as the "Device Class GUID."
 *   @return The Device Instance ID (linked below). A Device Instance ID has the form of:
 *           <device-id>\<instance-id>
 *
 *           Example: USB\VID_2109&PID_0813\7&3981c8d6&0&2
 *   @see https://learn.microsoft.com/en-us/windows-hardware/drivers/install/device-instance-ids
 *   @see https://learn.microsoft.com/en-us/windows/win32/api/cfgmgr32/nf-cfgmgr32-cm_get_device_interface_propertyw
 */
std::wstring map_path (LPCWSTR device_path) {
    ULONG buf_size = 0;
    DEVPROPTYPE type;
    CM_Get_Device_Interface_PropertyW(
        device_path, &DEVPKEY_Device_InstanceId, &type,
        nullptr, &buf_size, 0);

    std::vector<BYTE> buffer(buf_size);
    auto result = CM_Get_Device_Interface_PropertyW(
        device_path, &DEVPKEY_Device_InstanceId, &type,
        buffer.data(), &buf_size, 0);

    assert(result == CR_SUCCESS);
    assert(type == DEVPROP_TYPE_STRING);

    // buffer will be null-terminated
    return reinterpret_cast<wchar_t*>(buffer.data());
}

关于c++ - 从设备实例路径字符串中获取设备实例 DWORD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13563143/

相关文章:

c++ - 在 C++ 中使用默认参数跳过模板参数真的不可能吗,为什么语法另有建议?

c++ - 通用引用与非模板 `const&`

python - 我在 Windows Scheduler 中安排了一个 Python 脚本,但该脚本无法正常工作

c++ - SHFileOperation FO_MOVE 在目标驱动器已满时删除文件

c++ - Cin 没有得到正确的输入

c++ - 识别(编程)语言的关键字

windows - 如何在 Hyper-V 中与 VM 实例共享主机 VPN 连接?

c - Windows 互斥对象和信号量

c# - Windows 8 中的 Metro 应用程序如何与同一台计算机上的后端桌面应用程序通信?

c++ - win32 C++ 在屏幕上放置图像