检索受支持的服务格式

WpdServicesApiSample 应用程序包含的代码演示应用程序如何通过调用下表中接口的方法检索给定联系人服务支持的格式。

接口 说明
IPortableDeviceService 用于检索 IPortableDeviceServiceCapabilities 接口以访问支持的事件。
IPortableDeviceServiceCapabilities 提供对支持的事件和事件属性的访问。
IPortableDevicePropVariantCollection 包含受支持格式的列表。
IPortableDeviceValues 包含给定格式的属性。

 

当用户在命令行中选择选项“3”时,应用程序将调用 ServiceCapabilities.cpp 模块中的 ListSupportedFormats 方法。

请注意,在检索事件列表之前,示例应用程序会在连接的设备上打开联系人服务。

在 WPD 中,格式由属性描述,这些属性指定名称和 (可以选择) 给定格式的 MIME 类型。 这些属性在PortableDevice.h 头文件中定义。 有关支持的属性的说明,请参阅 属性 主题。

对于示例应用程序,如果 WpdServiceSampleDriver 是唯一安装的设备,驱动程序会为其 Contact 服务返回两种受支持的格式:“AbstractContactFormat”和“VCard2Format”。 这些格式对应于 PortableDevice.h 中的 WPD_OBJECT_FORMAT_ABSTRACT_CONTACTWPD_OBJECT_FORMAT_VCARD2 属性。

ServiceCapabilities.cpp 模块中的两种方法支持检索联系人服务支持的格式: ListSupportedFormatsDisplayFormat。 前者检索每种受支持格式的 GUID 标识符。 后者将此 GUID 转换为用户友好的字符串。

ListSupportedFormats 方法调用 IPortableDeviceService::Capabilities 方法来检索 IPortableDeviceServiceCapabilities 接口。 使用此接口,它通过调用 IPortableDeviceServiceCapabilities::GetSupportedFormats 方法检索支持的格式。 GetSupportedFormats 方法检索服务支持的每种格式的 GUID,并将该 GUID 复制到 IPortableDevicePropVariantCollection 对象中。

以下代码使用 ListSupportedFormats 方法。

// List all supported formats on the service
void ListSupportedFormats(
    IPortableDeviceService* pService)
{
    HRESULT hr              = S_OK;
    DWORD   dwNumFormats    = 0;
    CComPtr<IPortableDeviceServiceCapabilities>     pCapabilities;
    CComPtr<IPortableDevicePropVariantCollection>   pFormats;

    if (pService == NULL)
    {
        printf("! A NULL IPortableDeviceService interface pointer was received\n");
        return;
    }

    // Get an IPortableDeviceServiceCapabilities interface from the IPortableDeviceService interface to
    // access the service capabilities-specific methods.
    hr = pService->Capabilities(&pCapabilities);
    if (FAILED(hr))
    {
        printf("! Failed to get IPortableDeviceServiceCapabilities from IPortableDeviceService, hr = 0x%lx\n",hr);
    }

    // Get all formats supported by the service.
    if (SUCCEEDED(hr))
    {
        hr = pCapabilities->GetSupportedFormats(&pFormats);
        if (FAILED(hr))
        {
            printf("! Failed to get supported formats from the service, hr = 0x%lx\n",hr);
        }
    }

    // Get the number of supported formats found on the service.
    if (SUCCEEDED(hr))
    {
        hr = pFormats->GetCount(&dwNumFormats);
        if (FAILED(hr))
        {
            printf("! Failed to get number of supported formats, hr = 0x%lx\n",hr);
        }
    }

    printf("\n%d Supported Formats Found on the service\n\n", dwNumFormats);

    // Loop through each format and display it
    if (SUCCEEDED(hr))
    {
        for (DWORD dwIndex = 0; dwIndex < dwNumFormats; dwIndex++)
        {
            PROPVARIANT pv = {0};
            PropVariantInit(&pv);
            hr = pFormats->GetAt(dwIndex, &pv);

            if (SUCCEEDED(hr))
            {
                // We have a format.  It is assumed that
                // formats are returned as VT_CLSID VarTypes.
                if (pv.puuid != NULL)
                {
                    DisplayFormat(pCapabilities, *pv.puuid);
                    printf("\n");
                }
            }

            PropVariantClear(&pv);
        }
    }
}

ListSupportedFormats 方法检索给定服务支持的每种格式的 GUID 后,它将调用 DisplayFormat 方法以显示每种格式的脚本友好名称;例如“VCard2”。

DisplayFormat 方法调用 IPortableDeviceServiceCapabilities::GetFormatAttributes 方法检索给定格式 GUID 的属性集合。 然后,它调用 IPortableDeviceValues::GetStringValue 方法,并请求驱动程序返回给定格式的脚本友好名称。

以下代码使用 DisplayFormat 方法。

// Display basic information about a format
void DisplayFormat(
    IPortableDeviceServiceCapabilities* pCapabilities,
    REFGUID                             Format)
{
    CComPtr<IPortableDeviceValues> pAttributes;

    HRESULT hr = pCapabilities->GetFormatAttributes(Format, &pAttributes);

    if (SUCCEEDED(hr))
    {
        PWSTR pszFormatName = NULL;
        hr = pAttributes->GetStringValue(WPD_FORMAT_ATTRIBUTE_NAME, &pszFormatName);

        // Display the name of the format if it is available, otherwise fall back to displaying the GUID.
        if (SUCCEEDED(hr))
        {
            printf("%ws", pszFormatName);
        }
        else
        {
            printf("%ws", (PWSTR)CGuidToString(Format));
        }       

        CoTaskMemFree(pszFormatName);
        pszFormatName = NULL;
    }
}

IPortableDeviceService

IPortableDeviceServiceCapabilities

IPortableDeviceValues

WpdServicesApiSample