Share via


Enumeración del contenido del servicio

Una vez que la aplicación abra un servicio, puede comenzar a realizar operaciones relacionadas con el servicio. En el caso de la aplicación WpdServicesApiSample, una de estas operaciones es la enumeración de contenido de un servicio de contactos determinado. En la tabla siguiente se describen las interfaces usadas.

Interfaz Descripción
IPortableDeviceService Se usa para recuperar la interfaz IPortableDeviceContent2 para acceder al contenido del servicio.
IPortableDeviceContent2 Se usa para recuperar la interfaz IEnumPortableDeviceObjectIDs para enumerar objetos en el servicio.
IEnumPortableDeviceObjectIDs Se usa para enumerar objetos en el servicio.

 

El código de enumeración de contenido se encuentra en el módulo ContentEnumeration.cpp. Este código reside en los métodos EnumerateAllContent y RecursiveEnumerate . El método anterior llama al segundo.

El método EnumerateContent toma un puntero a un objeto IPortableDeviceService como su único parámetro. Este objeto corresponde a un servicio que la aplicación abrió anteriormente cuando llamó al método IPortableDeviceService::Open .

El método EnumerateContent crea un objeto IPortableDeviceContent2 y pasa este objeto al método IPortableDeviceService::Content . Este método, a su vez, recupera el contenido en el nivel raíz del servicio y, a continuación, comienza de forma recursiva a recuperar el contenido que se encuentra debajo de la raíz.

El código siguiente corresponde al método EnumerateContent .

// Enumerate all content on the service starting with the
// "DEVICE" object
void EnumerateAllContent(
    IPortableDeviceService* pService)
{
    HRESULT                          hr = S_OK;
    CComPtr<IPortableDeviceContent2> pContent;

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

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

    // Enumerate content starting from the "DEVICE" object.
    if (SUCCEEDED(hr))
    {
        printf("\n");
        RecursiveEnumerate(WPD_DEVICE_OBJECT_ID, pContent);
    }
}

El código siguiente corresponde al método RecursiveEnumerate . El método RecursiveEnumerate crea una instancia de una interfaz IEnumPortableDeviceObjectIDs para el objeto primario proporcionado y llama a IEnumPortableDeviceObjectIDs::Next, recuperando un lote de objetos secundarios inmediatos. Para cada objeto secundario, se llama a RecursiveEnumerate de nuevo para devolver sus objetos secundarios descendientes, etc.

// Recursively called function which enumerates using the specified
// object identifier as the parent.
void RecursiveEnumerate(
    PCWSTR                   pszObjectID,
    IPortableDeviceContent2* pContent)
{
    CComPtr<IEnumPortableDeviceObjectIDs> pEnumObjectIDs;

    // Print the object identifier being used as the parent during enumeration.
    printf("%ws\n",pszObjectID);

    // Get an IEnumPortableDeviceObjectIDs interface by calling EnumObjects with the
    // specified parent object identifier.
    HRESULT hr = pContent->EnumObjects(0,               // Flags are unused
                                       pszObjectID,     // Starting from the passed in object
                                       NULL,            // Filter is unused
                                       &pEnumObjectIDs);
    if (FAILED(hr))
    {
        printf("! Failed to get IEnumPortableDeviceObjectIDs from IPortableDeviceContent2, hr = 0x%lx\n",hr);
    }

    // Loop calling Next() while S_OK is being returned.
    while(hr == S_OK)
    {
        DWORD  cFetched = 0;
        PWSTR  szObjectIDArray[NUM_OBJECTS_TO_REQUEST] = {0};
        hr = pEnumObjectIDs->Next(NUM_OBJECTS_TO_REQUEST,   // Number of objects to request on each NEXT call
                                  szObjectIDArray,          // Array of PWSTR array which will be populated on each NEXT call
                                  &cFetched);               // Number of objects written to the PWSTR array
        if (SUCCEEDED(hr))
        {
            // Traverse the results of the Next() operation and recursively enumerate
            // Remember to free all returned object identifiers using CoTaskMemFree()
            for (DWORD dwIndex = 0; dwIndex < cFetched; dwIndex++)
            {
                RecursiveEnumerate(szObjectIDArray[dwIndex],pContent);

                // Free allocated PWSTRs after the recursive enumeration call has completed.
                CoTaskMemFree(szObjectIDArray[dwIndex]);
                szObjectIDArray[dwIndex] = NULL;
            }
        }
    }
}

IEnumPortableDeviceObjectIDs

IPortableDeviceContent2 (Interfaz)

IPortableDeviceService (Interfaz)

Apertura de un servicio

WpdServicesApiSample