如何在 C++ winrt 中取消装箱多值字符串
How to unbox multivalue string in c++ winrt
我可以使用
在网络上查找服务
std::vector<hstring> propertyKeys = std::vector<hstring>();
propertyKeys.push_back(L"System.Devices.Dnssd.HostName");
propertyKeys.push_back(L"System.Devices.IpAddress");
hstring aqsQueryString = L"System.Devices.AepService.ProtocolId:={4526e8c1-8aac-4153-9b16-55e86ada0e54} AND System.Devices.Dnssd.ServiceName:=\"_http._tcp\" AND System.Devices.Dnssd.Domain:=\"local\"";
auto watcher = DeviceInformation::CreateWatcher(aqsQueryString, propertyKeys, DeviceInformationKind::AssociationEndpointService);
然后我设置 Updated
回调以获取有关服务的信息。
watcher.Updated([](DeviceWatcher sender, DeviceInformationUpdate info) {
for(auto const& p : info.Properties()){
hstring s = unbox_value_or<hstring>(p.Value(), L""); // This unboxes the Hostname
auto ips = unbox_value_or<???>(p.Value(), ???); // How do I unbox this?
}
}
根据 Microsoft Documentation,System.Devices.IpAddress
的类型为 Multivalue String
。我试过 std::vector<hstring>
但那不起作用,因为类型必须是 winrt 类型。
我应该输入什么类型来拆箱多值字符串?
我设法使用 Sean Kellys 的回答解决了这个问题:
具体来说,我打印第一个 IP 的实现如下所示:
auto prop = info.Properties();
auto ips = prop.Lookup(L"System.Devices.IpAddress").as<Windows::Foundation::IReferenceArray<hstring>>();
winrt::com_array<hstring> arr;
ips.GetStringArray(arr);
std::wcout << arr[0].c_str() << std::endl;
我可以使用
在网络上查找服务std::vector<hstring> propertyKeys = std::vector<hstring>();
propertyKeys.push_back(L"System.Devices.Dnssd.HostName");
propertyKeys.push_back(L"System.Devices.IpAddress");
hstring aqsQueryString = L"System.Devices.AepService.ProtocolId:={4526e8c1-8aac-4153-9b16-55e86ada0e54} AND System.Devices.Dnssd.ServiceName:=\"_http._tcp\" AND System.Devices.Dnssd.Domain:=\"local\"";
auto watcher = DeviceInformation::CreateWatcher(aqsQueryString, propertyKeys, DeviceInformationKind::AssociationEndpointService);
然后我设置 Updated
回调以获取有关服务的信息。
watcher.Updated([](DeviceWatcher sender, DeviceInformationUpdate info) {
for(auto const& p : info.Properties()){
hstring s = unbox_value_or<hstring>(p.Value(), L""); // This unboxes the Hostname
auto ips = unbox_value_or<???>(p.Value(), ???); // How do I unbox this?
}
}
根据 Microsoft Documentation,System.Devices.IpAddress
的类型为 Multivalue String
。我试过 std::vector<hstring>
但那不起作用,因为类型必须是 winrt 类型。
我应该输入什么类型来拆箱多值字符串?
我设法使用 Sean Kellys 的回答解决了这个问题:
具体来说,我打印第一个 IP 的实现如下所示:
auto prop = info.Properties();
auto ips = prop.Lookup(L"System.Devices.IpAddress").as<Windows::Foundation::IReferenceArray<hstring>>();
winrt::com_array<hstring> arr;
ips.GetStringArray(arr);
std::wcout << arr[0].c_str() << std::endl;