【发布时间】:2017-05-10 03:05:51
【问题描述】:
如标题所示,我在 google 上搜索这个问题,但似乎无法通过 WPD(Windows Portable Device) api 获取序列号,在MSDN 中,我找到了 Portable Device 的 WPD_DEVICE_SERIAL_NUMBER 属性,可以有人告诉我如何使用 wpd api 获取此属性吗?
【问题讨论】:
如标题所示,我在 google 上搜索这个问题,但似乎无法通过 WPD(Windows Portable Device) api 获取序列号,在MSDN 中,我找到了 Portable Device 的 WPD_DEVICE_SERIAL_NUMBER 属性,可以有人告诉我如何使用 wpd api 获取此属性吗?
【问题讨论】:
一个过程。基本步骤如下:
IPortableDeviceValues
// Create our client information collection
ThrowIfFailed(CoCreateInstance(
CLSID_PortableDeviceValues,
nullptr,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&clientInfo)));
// We have to provide at the least our name, version, revision
ThrowIfFailed(clientInfo->SetStringValue(
WPD_CLIENT_NAME,
L"My super cool WPD client"));
ThrowIfFailed(clientInfo->SetUnsignedIntegerValue(
WPD_CLIENT_MAJOR_VERSION,
1));
ThrowIfFailed(clientInfo->SetUnsignedIntegerValue(
WPD_CLIENT_MINOR_VERSION,
0));
ThrowIfFailed(clientInfo->SetUnsignedIntegerValue(
WPD_CLIENT_REVISION,
1));
CoCreateInstance 获取IPortableDevice
// A WPD device is represented by an IPortableDevice instance
ThrowIfFailed(CoCreateInstance(
CLSID_PortableDevice,
nullptr,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&device)));
IPortableDevice::Open连接到设备,传递设备的ID和上面的客户端信息 device->Open(deviceId.c_str(), clientInfo);
IPortableDevice::Content 获取设备的IPortableDeviceContent
CComPtr<IPortableDeviceContent> retVal;
ThrowIfFailedWithMessage(
device.Content(&retVal),
L"! Failed to get IPortableDeviceContent from IPortableDevice");
IPortableDeviceContent::Properties 获取内容的IPortableDeviceProperties
CComPtr<IPortableDeviceProperties> retVal;
ThrowIfFailedWithMessage(
content.Properties(&retVal),
L"! Failed to get IPortableDeviceProperties from IPortableDeviceContent");
IPortableDeviceProperties::GetValues 获取属性的IPortableDeviceValues,将"DEVICE" 传递给pszObjectID,将nullptr 传递给pKeys
CComPtr<IPortableDeviceValues> retVal;
ThrowIfFailedWithMessage(
properties.GetValues(objectId.c_str(), nullptr, &retVal),
L"! Failed to get IPortableDeviceValues from IPortableDeviceProperties");
IPortableDeviceValues::GetStringValue 从值中获取序列号,将WPD_DEVICE_SERIAL_NUMBER 传递给key
propertyKey = WPD_DEVICE_SERIAL_NUMBER;
LPWSTR value = nullptr;
ThrowIfFailedWithMessage(
values.GetStringValue(propertyKey, &value),
L"! Failed to get string value from IPortableDeviceValues");
propertyValue = value;
if (value != nullptr)
{
CoTaskMemFree(value);
}
绝不是完整的列表,抱歉。 ThrowIf* 函数只是我写的从检查HRESULTs 到抛出异常的基本助手。希望这会为您指明正确的方向。
其他参考资料:
【讨论】: