如标题所示,我在 google 上搜索这个问题,但似乎无法通过 WPD(Windows Portable Device) api 获取序列号,在MSDN中,我找到了 Portable Device 的 WPD_DEVICE_SERIAL_NUMBER 属性,谁能告诉我如何使用 wpd api 获取此属性?
2 回答
0
有点过程。基本步骤如下:
- 获取并填充
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));
- 得到
IPortableDevice一个CoCreateInstance
// 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);
- 获取设备的
IPortableDeviceContent使用IPortableDevice::Content
CComPtr<IPortableDeviceContent> retVal;
ThrowIfFailedWithMessage(
device.Content(&retVal),
L"! Failed to get IPortableDeviceContent from IPortableDevice");
- 获取内容的
IPortableDeviceProperties使用IPortableDeviceContent::Properties
CComPtr<IPortableDeviceProperties> retVal;
ThrowIfFailedWithMessage(
content.Properties(&retVal),
L"! Failed to get IPortableDeviceProperties from IPortableDeviceContent");
- 获取属性的
IPortableDeviceValuesusingIPortableDeviceProperties::GetValues、传递"DEVICE"forpszObjectID和nullptrforpKeys
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 到抛出异常的基本助手。希望这会为您指明正确的方向。
附加参考:
于 2021-10-12T19:08:14.923 回答