1

如标题所示,我在 google 上搜索这个问题,但似乎无法通过 WPD(Windows Portable Device) api 获取序列号,在MSDN中,我找到了 Portable Device 的 WPD_DEVICE_SERIAL_NUMBER 属性,谁能告诉我如何使用 wpd api 获取此属性?

4

2 回答 2

0

C++ 示例可以在这里这里找到

于 2017-05-10T03:16:27.507 回答
0

有点过程。基本步骤如下:

  1. 获取并填充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));
  1. 得到IPortableDevice一个CoCreateInstance
    // A WPD device is represented by an IPortableDevice instance
    ThrowIfFailed(CoCreateInstance(
        CLSID_PortableDevice,
        nullptr,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&device)));
  1. 使用 连接到设备IPortableDevice::Open,传递设备的 ID 和上述客户端信息
    device->Open(deviceId.c_str(), clientInfo);
  1. 获取设备的IPortableDeviceContent使用IPortableDevice::Content
    CComPtr<IPortableDeviceContent> retVal;

    ThrowIfFailedWithMessage(
        device.Content(&retVal),
        L"! Failed to get IPortableDeviceContent from IPortableDevice");
  1. 获取内容的IPortableDeviceProperties使用IPortableDeviceContent::Properties
    CComPtr<IPortableDeviceProperties> retVal;

    ThrowIfFailedWithMessage(
        content.Properties(&retVal),
        L"! Failed to get IPortableDeviceProperties from IPortableDeviceContent");
  1. 获取属性的IPortableDeviceValuesusing IPortableDeviceProperties::GetValues、传递"DEVICE"forpszObjectIDnullptrforpKeys
    CComPtr<IPortableDeviceValues> retVal;

    ThrowIfFailedWithMessage(
        properties.GetValues(objectId.c_str(), nullptr, &retVal),
        L"! Failed to get IPortableDeviceValues from IPortableDeviceProperties");
  1. 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 回答