首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过win32 wpd接口获取序列号

如何通过win32 wpd接口获取序列号
EN

Stack Overflow用户
提问于 2017-05-10 11:05:51
回答 2查看 666关注 0票数 1

如标题所示,我在谷歌上搜索这个问题,但是似乎没有办法通过WPD(Windows Portable Device) api获得序列号,并且在MSDN中,我找到了便携设备的WPD_DEVICE_SERIAL_NUMBER属性,有人能告诉我如何使用wpd api获得这个属性吗?

EN

回答 2

Stack Overflow用户

发布于 2017-05-10 11:16:28

可以在herehere中找到C++示例

票数 0
EN

Stack Overflow用户

发布于 2021-10-12 19:08:15

这是一个小过程。基本步骤如下:

  1. 获取并填充客户端信息的IPortableDeviceValues

代码语言:javascript
复制
    // 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获取

  1. IPortableDevice

代码语言:javascript
复制
    // 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和上面的客户端信息

代码语言:javascript
复制
    device->Open(deviceId.c_str(), clientInfo);

使用IPortableDevice::Content获取设备的IPortableDeviceContent

代码语言:javascript
复制
    CComPtr<IPortableDeviceContent> retVal;

    ThrowIfFailedWithMessage(
        device.Content(&retVal),
        L"! Failed to get IPortableDeviceContent from IPortableDevice");

使用IPortableDeviceContent::Properties获取内容的IPortableDeviceProperties

代码语言:javascript
复制
    CComPtr<IPortableDeviceProperties> retVal;

    ThrowIfFailedWithMessage(
        content.Properties(&retVal),
        L"! Failed to get IPortableDeviceProperties from IPortableDeviceContent");

  1. 使用IPortableDeviceProperties::GetValues获取属性的IPortableDeviceValues,为pszObjectID传递"DEVICE",为pKeys

传递nullptr

代码语言:javascript
复制
    CComPtr<IPortableDeviceValues> retVal;

    ThrowIfFailedWithMessage(
        properties.GetValues(objectId.c_str(), nullptr, &retVal),
        L"! Failed to get IPortableDeviceValues from IPortableDeviceProperties");

通过为key传递WPD_DEVICE_SERIAL_NUMBER

  1. 使用IPortableDeviceValues::GetStringValue从值中获取序列号

代码语言:javascript
复制
    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到抛出异常的基本帮助器。希望这能将您引向正确的方向。

其他参考资料:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43882954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档