如何获取设备的设备接口 GUID? [英] How to get device interface GUID for a device?
本文介绍了如何获取设备的设备接口 GUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我列举了一台机器上的所有设备,如下所示:
I enumerate all the devices on a machine as follows:
HDEVINFO hDevList = SetupDiGetClassDevs( NULL, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT | DIGCF_ALLCLASSES /*| DIGCF_PROFILE*/ );
for ( int i = 0; true; ++i )
{
SP_DEVINFO_DATA devinfo_data = {0};
devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
if ( !SetupDiEnumDeviceInfo( hDevList, i, &devinfo_data ) )
break;
//TODO: get device interface GUID
//...
}
SetupDiDestroyDeviceInfoList( hDevList );
如何获取每个项目的设备界面 GUID?
How do I get device interface GUID for every item?
我试过了
SP_DEVICE_INTERFACE_DATA interface_data = {0};
interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
SetupDiEnumDeviceInterfaces( hDevList, &devinfo_data, ???, 0, &interface_data );
但它似乎需要第三个参数的实际 GUID.这实际上是我想要的!NULL 不是一个选项.
but it seems to require actual GUID for the third parameter. Which is actually what I want to get! NULL is not an option there.
推荐答案
您可以使用 统一设备属性模型在 Vista 及更高版本中可用.它具有 DEVPKEY_Device_Class 和 DEVPKEY_Device_ClassGuid 属性:
You can get a lot of information using the Unified Device Property Model available in Vista and higher. It has the DEVPKEY_Device_Class and DEVPKEY_Device_ClassGuid properties:
HDEVINFO list = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT | DIGCF_ALLCLASSES /*| DIGCF_PROFILE*/);
for (int i = 0; true; ++i)
{
SP_DEVINFO_DATA data = { 0 };
data.cbSize = sizeof(SP_DEVINFO_DATA);
if (!SetupDiEnumDeviceInfo(list, i, &data))
break;
// get name property
DEVPROPTYPE type;
DWORD size = 0;
SetupDiGetDeviceProperty(list, &data, &DEVPKEY_NAME, &type, NULL, 0, &size, 0);
if (size > 0)
{
LPWSTR name = (LPWSTR)malloc(size);
SetupDiGetDeviceProperty(list, &data, &DEVPKEY_NAME, &type, (PBYTE)name, size, &size, 0);
wprintf(L"name: %s\n", name);
free(name);
}
// get class name
SetupDiGetDeviceProperty(list, &data, &DEVPKEY_Device_Class, &type, NULL, 0, &size, 0);
if (size > 0)
{
LPWSTR name = (LPWSTR)malloc(size);
SetupDiGetDeviceProperty(list, &data, &DEVPKEY_Device_Class, &type, (PBYTE)name, size, &size, 0);
wprintf(L" class: %s\n", name);
free(name);
}
// get class guid
SetupDiGetDeviceProperty(list, &data, &DEVPKEY_Device_ClassGuid, &type, NULL, 0, &size, 0);
if (size > 0)
{
GUID* guid = (GUID*)malloc(size);
SetupDiGetDeviceProperty(list, &data, &DEVPKEY_Device_ClassGuid, &type, (PBYTE)guid, size, &size, 0);
wchar_t name[64];
StringFromGUID2(*guid, (LPOLESTR)name, ARRAYSIZE(name));
wprintf(L" class guid: %s\n", name);
free(guid);
}
}
SetupDiDestroyDeviceInfoList(list);
这将输出如下内容:
name: ACPI Fan
class: System
class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318}
name: ACPI Fan
class: System
class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318}
name: ACPI Fan
class: System
class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318}
name: ACPI Fan
class: System
class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318}
name: ACPI Fan
class: System
class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318}
name: Microsoft Hyper-V Virtual Machine Bus Provider
class: System
class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318}
name: Plug and Play Software Device Enumerator
class: System
class guid: {4D36E97D-E325-11CE-BFC1-08002BE10318}
etc...
这篇关于如何获取设备的设备接口 GUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文