共用方式為


擷取支援的服務格式

WpdServicesApiSample 應用程式包含程式代碼,示範應用程式如何藉由呼叫下表中介面的方法,擷取指定聯繫人服務所支援的格式。

介面 描述
IPortableDeviceService 用於擷取 IPortableDeviceServiceCapabilities 介面,以便存取支援的事件。
IPortableDeviceServiceCapabilities 提供支援事件和事件屬性的存取權。
IPortableDevicePropVariantCollection 包含支援的格式清單。
IPortableDeviceValues 包含指定格式的屬性。

 

當使用者在命令行選擇 「3」 選項時,應用程式會叫用 ServiceCapabilities.cpp 模組中找到的 ListSupportedFormats 方法。

請注意,在擷取事件清單之前,範例應用程式會在連線的裝置上開啟聯繫人服務。

在 WPD 中,格式由屬性所描述,而這些屬性指定格式名稱及(可選)MIME 類型。 這些屬性定義於PortableDevice.h 頭檔中。 如需支援屬性的描述,請參閱 屬性 主題。

在範例應用程式中,如果 WpdServiceSampleDriver 是唯一安裝的裝置,驅動程式會傳回其 Contact Service 的兩種支援格式:“AbstractContactFormat” 和 “VCard2Format”。 這些格式會對應至 PortableDevice.h 中找到的 WPD_OBJECT_FORMAT_ABSTRACT_CONTACTWPD_OBJECT_FORMAT_VCARD2 屬性。

ServiceCapabilities.cpp模組中的兩種方法支援擷取 Contacts 服務支援的格式: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