从设备实例路径字符串获取设备实例 DWORD
Get device instance DWORD from device instance path string
我得到一个设备实例路径,如下所示
L"\\?\USB#VID_0403&PID_6001#6&2cc2d230&0&2#{219d0508-57a8-4ff5-97a1-bd86587c6c7e}"
from 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);
和其他一些设置...巫毒教没有成功。非常感谢任何帮助,因为 - 如前所述 - 我已经几个小时无法绕过这种疯狂了,尽管有几十个相反的例子(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: USBVID_2109&PID_08137&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());
}
正如你所说,没有直接的方法。
但是,您应该能够通过一些字符串编辑从设备路径/设备接口 ID 中获取设备实例 ID,请执行以下步骤:
- 完全卸下起始
\\?\
部分,直到USB
。 - 完全删除最后一个
{...}
部件 - 将
#
替换为
起点
"\\?\USB#VID_0403&PID_6001#6&2cc2d230&0&2#{219d0508-57a8-4ff5-97a1-bd86587c6c7e}"
你现在应该有
"USBVID_0403&PID_60016&2cc2d230&0&2"
这应该是有效的设备实例 ID。如果不是,请尝试删除最后一个"\"。
然后,你可以把它喂给CM_Locate_DevNode()并得到想要的DEVINST。
相关文章:
- 从C++实例化QML
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 建议在运行时将带有类实例的列表从c++导入qml
- 约束和显式模板实例化
- 通过实例理解std::move及其目的
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 正在生成未知类实例
- 从DLL中删除类的实例
- 在std::vector上存储带有模板的类实例
- 从设备实例路径字符串获取设备实例 DWORD