Mengambil Peristiwa Layanan yang Didukung

Aplikasi WpdServicesApiSample menyertakan kode yang menunjukkan bagaimana aplikasi dapat mengambil peristiwa yang didukung oleh layanan Kontak tertentu dengan memanggil metode pada antarmuka berikut.

Antarmuka Deskripsi
IPortableDeviceService Digunakan untuk mengambil antarmuka IPortableDeviceServiceCapabilities untuk mengakses peristiwa yang didukung.
IPortableDeviceServiceCapabilities Menyediakan akses ke peristiwa dan atribut peristiwa yang didukung.
IPortableDevicePropVariantCollection Berisi daftar peristiwa yang didukung.
IPortableDeviceValues Berisi atribut untuk peristiwa tertentu.

 

Ketika pengguna memilih opsi "4" di baris perintah, aplikasi memanggil metode ListSupportedEvents yang ditemukan dalam modul ServiceCapabilities.cpp.

Perhatikan bahwa sebelum mengambil daftar peristiwa, aplikasi sampel membuka layanan Kontak pada perangkat yang terhubung.

Dalam WPD, peristiwa dijelaskan dengan nama, opsi, dan parameternya. Nama peristiwa adalah string ramah skrip, misalnya, "MyCustomEvent". Opsi peristiwa menunjukkan apakah peristiwa tertentu disiarkan ke semua klien dan apakah peristiwa tersebut mendukung pemutaran otomatis. Atribut parameter peristiwa mencakup PROPERTYKEY dan VARTYPE parameter tertentu.

Empat metode dalam modul ServiceCapabilities.cpp mendukung pengambilan peristiwa yang didukung untuk layanan Kontak tertentu: ListSupportedEvents, DisplayEvent, DisplayEventOptions, dan DisplayEventParameters. Metode ListSupportedEvents mengambil hitungan peristiwa yang didukung dan pengidentifikasi GUID untuk setiap peristiwa. Metode DisplayEvent menampilkan nama peristiwa atau GUID, lalu memanggil DisplayEventOptions dan DisplayEventParameters untuk menampilkan data terkait peristiwa.

Metode ListSupportedEvents memanggil metode IPortableDeviceService::Capabilities untuk mengambil antarmuka IPortableDeviceServiceCapabilities . Dengan menggunakan antarmuka ini, ia mengambil peristiwa yang didukung dengan memanggil metode IPortableDeviceServiceCapabilities::GetSupportedEvents . Metode GetSupportedEvents mengambil GUID untuk setiap peristiwa yang didukung oleh layanan dan menyalin GUID tersebut ke dalam objek IPortableDevicePropVariantCollection .

Kode berikut mengilustrasikan pengambilan peristiwa layanan yang didukung.

// List all supported events on the service
void ListSupportedEvents(
    IPortableDeviceService* pService)
{
    HRESULT hr          = S_OK;
    DWORD   dwNumEvents = 0;
    CComPtr<IPortableDeviceServiceCapabilities>     pCapabilities;
    CComPtr<IPortableDevicePropVariantCollection>   pEvents;

    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 events supported by the service.
    if (SUCCEEDED(hr))
    {
        hr = pCapabilities->GetSupportedEvents(&pEvents);
        if (FAILED(hr))
        {
            printf("! Failed to get supported events from the service, hr = 0x%lx\n",hr);
        }
    }

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

    printf("\n%d Supported Events Found on the service\n\n", dwNumEvents);

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

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

            PropVariantClear(&pv);
        }
    }
}

Setelah metode ListSupportedEvents mengambil GUID yang mewakili setiap peristiwa yang didukung oleh layanan tertentu, metode DisplayEvent akan memanggil metode DisplayEvent untuk mengambil data khusus peristiwa. Data ini mencakup nama peristiwa, opsinya (siaran atau pemutaran otomatis), GUID parameter, jenis parameter, dan sebagainya.

Metode DisplayEvent memanggil metode IPortableDeviceServiceCapabilities::GetEventAttributes untuk mengambil kumpulan atribut untuk peristiwa tertentu. Kemudian memanggil metode IPortableDeviceValues::GetStringValue dan meminta agar driver mengembalikan nama yang mudah digunakan untuk peristiwa tertentu. Selanjutnya, DisplayEvent memanggil IPortableDeviceValues::GetIPortableDeviceValuesValue untuk mengambil opsi peristiwa. Terakhir, DisplayEvent memanggil IPortableDeviceValues::GetIPortableDeviceKeyCollectionValue untuk mengambil daftar parameter peristiwa. Ini meneruskan data yang dikembalikan oleh metode ini ke DisplayEventOptions dan fungsi pembantu DisplayEventParameters , yang merender opsi dan informasi parameter untuk peristiwa tertentu.

Kode berikut menggunakan metode DisplayEvent .

// Display basic information about an event
void DisplayEvent(
    IPortableDeviceServiceCapabilities* pCapabilities,
    REFGUID                             Event)
{
    CComPtr<IPortableDeviceValues> pAttributes;

    // Get the event attributes which describe the event
    HRESULT hr = pCapabilities->GetEventAttributes(Event, &pAttributes);
    if (FAILED(hr))
    {
        printf("! Failed to get the event attributes, hr = 0x%lx\n",hr);
    }

    if (SUCCEEDED(hr))
    {
        PWSTR pszFormatName = NULL;
        CComPtr<IPortableDeviceValues>          pOptions;
        CComPtr<IPortableDeviceKeyCollection>   pParameters;

        // Display the name of the event if it is available. Otherwise, fall back to displaying the GUID.
        hr = pAttributes->GetStringValue(WPD_EVENT_ATTRIBUTE_NAME, &pszFormatName);
        if (SUCCEEDED(hr))
        {
            printf("%ws\n", pszFormatName);
        }
        else
        {
            printf("%ws\n", (PWSTR)CGuidToString(Event));
        }       

        // Display the event options
        hr = pAttributes->GetIPortableDeviceValuesValue(WPD_EVENT_ATTRIBUTE_OPTIONS, &pOptions);
        if (SUCCEEDED(hr))
        {
            DisplayEventOptions(pOptions);
        }
        else
        {
            printf("! Failed to get the event options, hr = 0x%lx\n", hr);
        }       

        // Display the event parameters
        hr = pAttributes->GetIPortableDeviceKeyCollectionValue(WPD_EVENT_ATTRIBUTE_PARAMETERS, &pParameters);
        if (SUCCEEDED(hr))
        {
            DisplayEventParameters(pCapabilities, Event, pParameters);
        }
        else
        {
            printf("! Failed to get the event parameters, hr = 0x%lx\n", hr);
        }       
        
        CoTaskMemFree(pszFormatName);
        pszFormatName = NULL;
    }
}

Fungsi pembantu DisplayEventOptions menerima objek IPortableDeviceValues yang berisi data opsi peristiwa. Kemudian memanggil metode IPortableDeviceValues::GetBoolValue untuk mengambil data opsi. Dengan menggunakan data ini, ini merender string yang menunjukkan apakah opsi siaran dan pemutaran otomatis didukung.

// Display the basic event options.
void DisplayEventOptions(
    IPortableDeviceValues* pOptions)
{
    printf("\tEvent Options:\n");
    // Read the WPD_EVENT_OPTION_IS_BROADCAST_EVENT value to see if the event is
    // a broadcast event. If the read fails, assume FALSE
    BOOL  bIsBroadcastEvent = FALSE;
    pOptions->GetBoolValue(WPD_EVENT_OPTION_IS_BROADCAST_EVENT, &bIsBroadcastEvent);
    printf("\tWPD_EVENT_OPTION_IS_BROADCAST_EVENT = %ws\n",bIsBroadcastEvent ? L"TRUE" : L"FALSE");
}

IPortableDeviceKeyCollection

IPortableDeviceService

IPortableDeviceServiceCapabilities

IPortableDeviceValues

WpdServicesApiSample