怎么获得USB设备的Location Info信息?在UsbTreeView可以看到

twohorses 2019-07-13 09:00:37
在UsbTreeView软件里可以看到Location Info信息,如下图的红色线标识的地方,可是我用SetupDiGetClassDevs,SetupDiEnumDeviceInterfaces,SetupDiGetDeviceInstanceId,SetupDiGetDeviceRegistryPropertyA,等函数都用了,怎么就是找不到这个Location Info信息?另外用黄色标记的地方,怎么有两个Device ID?而且还不一样,我只能获得第二个Device ID,怎么能获得第一个Device ID?
...全文
1433 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2021-01-18
  • 打赏
  • 举报
回复
system调用devcon?
rc520ll 2021-01-16
  • 打赏
  • 举报
回复
你好,我想问一下你的问题解决了吗, 我现在也遇到同样的问题,Port_#0003.Hub_#0002怎么获取呢
rc520ll 2020-11-20
  • 打赏
  • 举报
回复
想问一下你这个问题解决了吗,我现在也有这个需求
BrillianceRen 2019-08-06
  • 打赏
  • 举报
回复
不清楚,只能到这了
twohorses 2019-08-06
  • 打赏
  • 举报
回复
引用 9 楼 BrillianceRen 的回复:
[quote=引用 8 楼 twohorses 的回复:]
[quote=引用 6 楼 BrillianceRen 的回复:]
SetupDiGetDeviceProperty的DEVPKEY_Device_Parent属性是USB HUB,DEVPKEY_Device_LocationInfo属性是USB Port.


请教一下,SetupDiGetDeviceProperty这个函数要获得DEVPKEY_Device_LocationInfo属性,DEVPROPKEY这个参数怎么设置?我在网上看了几个例子,设置这个参数的.fmtid和.pid写的都是固定的,但是还不一样,我不知道这个怎么获得?我设备的guid是通过 Result = HidD_GetHidGuid(HidGuid)这个获得的,
[/quote]


#include <Windows.h>
#include <initguid.h>
#include <devpkey.h>
#include <cfgmgr32.h>
#include <SetupAPI.h>

#pragma comment (lib, "setupapi.lib")

#include <iostream>
using namespace std;

int main()
{
HDEVINFO DeviceInfoSet = SetupDiGetClassDevsW(NULL, L"USB", NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
return 0;
for (int i = 0; ; i++) {
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
if (!SetupDiEnumDeviceInfo(DeviceInfoSet, i, &DeviceInfoData))
break;
DWORD RequiredSize = 0;
DEVPROPTYPE PropertyType = 0;
WCHAR PropertyBuffer[4096] = { 0 };
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc, &PropertyType, reinterpret_cast<PBYTE>(PropertyBuffer), sizeof(PropertyBuffer), &RequiredSize, 0))
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_LocationInfo, &PropertyType, reinterpret_cast<PBYTE>(PropertyBuffer), sizeof(PropertyBuffer), &RequiredSize, 0))
wcout << L" Device Location Info: \"" << PropertyBuffer << L"\"" << endl;
}
return 0;
}
[/quote]

谢谢你的回复,这个可以得到USB的端口信息,但是我的设备是Hid设备,应该是这个USB下面的一个子设备,怎么通过Hid设备找到父设备USB?我在网上查到要用到这几个函数:CM_Locate_DevNode,CM_Get_Parent,CM_Get_DevNode_Property ,但是CM_Get_Parent(ref IntPtr pdnDevInst, IntPtr dnDevInst, uint ulFlags)这个函数中的IntPtr dnDevInst这个参数我不知道怎么获得?尝试了用CM_Locate_DevNode这个函数获得,没有成功
twohorses 2019-08-06
  • 打赏
  • 举报
回复
引用 9 楼 BrillianceRen 的回复:
[quote=引用 8 楼 twohorses 的回复:]
[quote=引用 6 楼 BrillianceRen 的回复:]
SetupDiGetDeviceProperty的DEVPKEY_Device_Parent属性是USB HUB,DEVPKEY_Device_LocationInfo属性是USB Port.


请教一下,SetupDiGetDeviceProperty这个函数要获得DEVPKEY_Device_LocationInfo属性,DEVPROPKEY这个参数怎么设置?我在网上看了几个例子,设置这个参数的.fmtid和.pid写的都是固定的,但是还不一样,我不知道这个怎么获得?我设备的guid是通过 Result = HidD_GetHidGuid(HidGuid)这个获得的,
[/quote]


#include <Windows.h>
#include <initguid.h>
#include <devpkey.h>
#include <cfgmgr32.h>
#include <SetupAPI.h>

#pragma comment (lib, "setupapi.lib")

#include <iostream>
using namespace std;

int main()
{
HDEVINFO DeviceInfoSet = SetupDiGetClassDevsW(NULL, L"USB", NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
return 0;
for (int i = 0; ; i++) {
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
if (!SetupDiEnumDeviceInfo(DeviceInfoSet, i, &DeviceInfoData))
break;
DWORD RequiredSize = 0;
DEVPROPTYPE PropertyType = 0;
WCHAR PropertyBuffer[4096] = { 0 };
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc, &PropertyType, reinterpret_cast<PBYTE>(PropertyBuffer), sizeof(PropertyBuffer), &RequiredSize, 0))
if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_LocationInfo, &PropertyType, reinterpret_cast<PBYTE>(PropertyBuffer), sizeof(PropertyBuffer), &RequiredSize, 0))
wcout << L" Device Location Info: \"" << PropertyBuffer << L"\"" << endl;
}
return 0;
}
[/quote]

谢谢你的回复,这个可以得到USB的端口信息,但是我的设备是Hid设备,应该是这个USB下面的一个子设备,怎么通过Hid设备找到父设备USB?我在网上查到要用到这几个函数:CM_Locate_DevNode,CM_Get_Parent,CM_Get_DevNode_Property ,但是CM_Get_Parent(ref IntPtr pdnDevInst, IntPtr dnDevInst, uint ulFlags)这个函数中的IntPtr dnDevInst这个参数我不知道怎么获得?尝试了用CM_Locate_DevNode这个函数获得,没有成功
BrillianceRen 2019-08-05
  • 打赏
  • 举报
回复
引用 8 楼 twohorses 的回复:
[quote=引用 6 楼 BrillianceRen 的回复:] SetupDiGetDeviceProperty的DEVPKEY_Device_Parent属性是USB HUB,DEVPKEY_Device_LocationInfo属性是USB Port.
请教一下,SetupDiGetDeviceProperty这个函数要获得DEVPKEY_Device_LocationInfo属性,DEVPROPKEY这个参数怎么设置?我在网上看了几个例子,设置这个参数的.fmtid和.pid写的都是固定的,但是还不一样,我不知道这个怎么获得?我设备的guid是通过 Result = HidD_GetHidGuid(HidGuid)这个获得的, [/quote]

#include <Windows.h>
#include <initguid.h>
#include <devpkey.h>
#include <cfgmgr32.h>
#include <SetupAPI.h>

#pragma comment (lib, "setupapi.lib")

#include <iostream>
using namespace std;

int main()
{
	HDEVINFO DeviceInfoSet = SetupDiGetClassDevsW(NULL, L"USB", NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
	if (DeviceInfoSet == INVALID_HANDLE_VALUE)
		return 0;
	for (int i = 0; ; i++) {
		SP_DEVINFO_DATA DeviceInfoData;
		DeviceInfoData.cbSize = sizeof(DeviceInfoData);
		if (!SetupDiEnumDeviceInfo(DeviceInfoSet, i, &DeviceInfoData))
			break;
		DWORD RequiredSize = 0;
		DEVPROPTYPE PropertyType = 0;
		WCHAR PropertyBuffer[4096] = { 0 };
		if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc, &PropertyType, reinterpret_cast<PBYTE>(PropertyBuffer), sizeof(PropertyBuffer), &RequiredSize, 0))
			if (SetupDiGetDevicePropertyW(DeviceInfoSet, &DeviceInfoData, &DEVPKEY_Device_LocationInfo, &PropertyType, reinterpret_cast<PBYTE>(PropertyBuffer), sizeof(PropertyBuffer), &RequiredSize, 0)) 
				wcout << L"    Device Location Info: \"" << PropertyBuffer << L"\"" << endl;
	}
	return 0;
}
twohorses 2019-07-30
  • 打赏
  • 举报
回复
引用 6 楼 BrillianceRen 的回复:
SetupDiGetDeviceProperty的DEVPKEY_Device_Parent属性是USB HUB,DEVPKEY_Device_LocationInfo属性是USB Port.


请教一下,SetupDiGetDeviceProperty这个函数要获得DEVPKEY_Device_LocationInfo属性,DEVPROPKEY这个参数怎么设置?我在网上看了几个例子,设置这个参数的.fmtid和.pid写的都是固定的,但是还不一样,我不知道这个怎么获得?我设备的guid是通过 Result = HidD_GetHidGuid(HidGuid)这个获得的,
twohorses 2019-07-29
  • 打赏
  • 举报
回复
引用 6 楼 BrillianceRen 的回复:
SetupDiGetDeviceProperty的DEVPKEY_Device_Parent属性是USB HUB,DEVPKEY_Device_LocationInfo属性是USB Port.

谢谢,这个函数我用过,没找到端口信息,我待会再试一下,看是不是以前漏掉了

BrillianceRen 2019-07-27
  • 打赏
  • 举报
回复
SetupDiGetDeviceProperty的DEVPKEY_Device_Parent属性是USB HUB,DEVPKEY_Device_LocationInfo属性是USB Port.
twohorses 2019-07-27
  • 打赏
  • 举报
回复
这方面的信息有点少
twohorses 2019-07-15
  • 打赏
  • 举报
回复
引用 2 楼 twohorses 的回复:
自己顶一下,希望高手指导一下

谢谢你的帮助,SetupDiGetClassDevs ,SetupDiEnumDeviceInfo ,SetupDiGetDeviceInstanceId这三个函数我都用了,获得的信息不是很多,CM_Get_Parent这个函数没用过,我待会试试,其实我是想得到usb口的端口号,在UsbTreeView左边目录树里有对应的端口号,我一直没有获得这个端口号,所以上面截图没有截目录树,在右边的父系中Location Info中的port #0002对应的应该就是这个端口号,所以才想到获得这个信息,如下图,如果能直接获得左边目录树里的Port2那是最好了。
636f6c696e 2019-07-13
  • 打赏
  • 举报
回复
https://docs.microsoft.com/en-us/previous-versions/ff537421(v=vs.85) 找找吧,从描述上看应该是IOCTL_INTERNAL_USB_GET_TOPOLOGY_ADDRESS
轻箬笠 2019-07-13
  • 打赏
  • 举报
回复
第一个device id应该是父系属性

SetupDiGetClassDevs 取得某类设备集合比如在线USB设备集
SetupDiEnumDeviceInfo 依次枚举上面的集合,对每个设备有对应的信息,比如实例ID
SetupDiGetDeviceInstanceId
CM_Get_Parent 取得某个设备实例的“父系”实例
twohorses 2019-07-13
  • 打赏
  • 举报
回复
自己顶一下,希望高手指导一下

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧