如标题所示,我在谷歌上搜索这个问题,但是似乎没有办法通过WPD(Windows Portable Device) api获得序列号,并且在MSDN中,我找到了便携设备的WPD_DEVICE_SERIAL_NUMBER属性,有人能告诉我如何使用wpd api获得这个属性吗?
发布于 2021-10-12 19:08:15
这是一个小过程。基本步骤如下:
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,为pszObjectID传递"DEVICE",为pKeys传递nullptr
CComPtr<IPortableDeviceValues> retVal;
ThrowIfFailedWithMessage(
properties.GetValues(objectId.c_str(), nullptr, &retVal),
L"! Failed to get IPortableDeviceValues from IPortableDeviceProperties");通过为key传递WPD_DEVICE_SERIAL_NUMBER,
IPortableDeviceValues::GetStringValue从值中获取序列号 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*函数只是我编写的从检查HRESULT到抛出异常的基本帮助器。希望这能将您引向正确的方向。
其他参考资料:
https://stackoverflow.com/questions/43882954
复制相似问题