为什么我的 DeviceInformation 对象没有 System.Devices.InterfaceClassGuid 属性?

Why don't my DeviceInformation objects have a System.Devices.InterfaceClassGuid property?

本文关键字:Devices InterfaceClassGuid 属性 System 我的 DeviceInformation 对象 为什么      更新时间:2023-10-16

我正在尝试在桌面应用程序中使用 C++/WinRT 枚举系统上的所有设备:

auto devices = winrt::Windows::Devices::Enumeration::DeviceInformation::FindAllAsync().get();
for (int i = 0; i < devices.Size(); i ++)
{
auto device = devices.GetAt(i);
auto props = device.Properties();
if (props.HasKey(L"System.Devices.InterfaceClassGuid")) {
// Get the GUID and print it
}
}

这篇 MSDN 文章指出:

如果未指定 DeviceInformationKind,

或者所使用的方法未提供 DeviceInformationKind 参数,则默认类型为 DeviceInterface。

这篇 MSDN 文章将System.Devices.InterfaceClassGuid列为DeviceInterface的属性。

但是,FindAllAsync返回的DeviceInformation对象都没有System.Devices.InterfaceClassGuid属性。我在这里做错了什么?

事实证明,我错过了第二篇文章中的重要部分:

还可以使用这些属性来指示要为每个设备返回哪些信息。这使您能够指定返回到应用程序的设备信息。

实际上,您必须指定要返回的属性。这有效:

std::vector<winrt::hstring> additionalProperties = {
L"System.Devices.InterfaceClassGuid"
};
auto devices = winrt::Windows::Devices::Enumeration::DeviceInformation::FindAllAsync(L"", std::move(additionalProperties)).get();