【问题标题】:how to get serial number via win32 wpd api如何通过win32 wpd api获取序列号
【发布时间】:2017-05-10 03:05:51
【问题描述】:

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

【问题讨论】:

    标签: winapi wpd


    【解决方案1】:

    C++ 示例可以在herehere 找到

    【解决方案2】:

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

    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. 使用CoCreateInstance 获取IPortableDevice
        // 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. 使用IPortableDevice::Content 获取设备的IPortableDeviceContent
        CComPtr<IPortableDeviceContent> retVal;
    
        ThrowIfFailedWithMessage(
            device.Content(&retVal),
            L"! Failed to get IPortableDeviceContent from IPortableDevice");
    
    1. 使用IPortableDeviceContent::Properties 获取内容的IPortableDeviceProperties
        CComPtr<IPortableDeviceProperties> retVal;
    
        ThrowIfFailedWithMessage(
            content.Properties(&retVal),
            L"! Failed to get IPortableDeviceProperties from IPortableDeviceContent");
    
    1. 使用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");
    
    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 到抛出异常的基本助手。希望这会为您指明正确的方向。

    其他参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      相关资源
      最近更新 更多