擷取支援的服務格式

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

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

 

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

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

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

在範例應用程式中,如果 WpdServiceSampleDriver 是唯一安裝的裝置,驅動程式會傳回其 Contact Service 的兩種支援格式:「AbstractContactFormat」 和 「VCard2Format」。 這些格式會對應到 PortableDevice.h 中找到的WPD_OBJECT_FORMAT_ABSTRACT_CONTACT和WPD_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