Share via


Transferencia de contenido desde el dispositivo a un equipo

Una operación común realizada por una aplicación WPD es la transferencia de contenido desde un dispositivo conectado al equipo.

Las transferencias de contenido se realizan mediante las interfaces descritas en la tabla siguiente.

Interfaz Descripción
IPortableDeviceContent (Interfaz) Proporciona acceso a la interfaz IPortableDeviceProperties .
IPortableDeviceProperties (interfaz) Proporciona acceso a métodos específicos de la propiedad.
IPortableDeviceResources (Interfaz) Se usa para almacenar las claves de propiedad para el perfil especificado.
IStream (interfaz) Se usa para leer y escribir los datos.

 

La TransferContentFromDevice función del módulo ContentTransfer.cpp de la aplicación de ejemplo muestra cómo una aplicación podría transferir información de contacto desde un dispositivo conectado a un equipo.

La primera tarea realizada por la TransferContentFromDevice función es pedir al usuario que escriba un identificador de objeto para el objeto primario en el dispositivo (en el que se transferirá el contenido).

HRESULT                            hr                   = S_OK;
WCHAR                              szSelection[81]      = {0};
CComPtr<IPortableDeviceContent>    pContent;
CComPtr<IPortableDeviceResources>  pResources;
CComPtr<IPortableDeviceProperties> pProperties;
CComPtr<IStream>                   pObjectDataStream;
CComPtr<IStream>                   pFinalFileStream;
DWORD                              cbOptimalTransferSize = 0;
CAtlStringW                        strOriginalFileName;

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


// Prompt user to enter an object identifier on the device to transfer.
printf("Enter the identifer of the object you wish to transfer.\n>");
hr = StringCbGetsW(szSelection,sizeof(szSelection));
if (FAILED(hr))
{
    printf("An invalid object identifier was specified, aborting content transfer\n");
}

El siguiente paso es la recuperación de un objeto IPortableDeviceContent que el ejemplo usa para acceder a los métodos específicos del contenido.

if (SUCCEEDED(hr))
{
    hr = pDevice->Content(&pContent);
    if (FAILED(hr))
    {
        printf("! Failed to get IPortableDeviceContent from IPortableDevice, hr = 0x%lx\n",hr);
    }
}

El siguiente paso es la recuperación de un objeto IPortableDeviceResources que el ejemplo usa para acceder a los métodos específicos del recurso.

if (SUCCEEDED(hr))
{
    hr = pContent->Transfer(&pResources);
    if (FAILED(hr))
    {
        printf("! Failed to get IPortableDeviceResources from IPortableDeviceContent, hr = 0x%lx\n",hr);
    }
}

El siguiente paso es la recuperación de un objeto IStream que el ejemplo usa para leer los datos que transfiere desde el dispositivo.

if (SUCCEEDED(hr))
{
    hr = pResources->GetStream(szSelection,             // Identifier of the object we want to transfer
                               WPD_RESOURCE_DEFAULT,    // We are transferring the default resource (which is the entire object's data)
                               STGM_READ,               // Opening a stream in READ mode, because we are reading data from the device.
                               &cbOptimalTransferSize,  // Driver supplied optimal transfer size
                               &pObjectDataStream);
    if (FAILED(hr))
    {
        printf("! Failed to get IStream (representing object data on the device) from IPortableDeviceResources, hr = 0x%lx\n",hr);
    }
}

El siguiente paso es la recuperación del nombre de archivo del objeto en el dispositivo. Esta cadena se usa para crear el nombre de archivo correspondiente en el equipo. Si el objeto no tiene un nombre de archivo en el dispositivo, el identificador del objeto se convierte en una cadena y se usa para crear el nombre de archivo de destino.

if (SUCCEEDED(hr))
{
    hr = pContent->Properties(&pProperties);
    if (SUCCEEDED(hr))
    {
        hr = GetStringValue(pProperties,
                            szSelection,
                            WPD_OBJECT_ORIGINAL_FILE_NAME,
                            strOriginalFileName);
        if (FAILED(hr))
        {
            printf("! Failed to read WPD_OBJECT_ORIGINAL_FILE_NAME on object '%ws', hr = 0x%lx\n", szSelection, hr);
            strOriginalFileName.Format(L"%ws.data", szSelection);
            printf("* Creating a filename '%ws' as a default.\n", (PWSTR)strOriginalFileName.GetString());
            // Set the HRESULT to S_OK, so we can continue with our newly generated
            // temporary file name.
            hr = S_OK;
        }
    }
    else
    {
        printf("! Failed to get IPortableDeviceProperties from IPortableDeviceContent, hr = 0x%lx\n", hr);
    }
}

Después de esto, el ejemplo crea un objeto IStream de destino.

if (SUCCEEDED(hr))
{
    hr = SHCreateStreamOnFile(strOriginalFileName, STGM_CREATE|STGM_WRITE, &pFinalFileStream);
    if (FAILED(hr))
    {
        printf("! Failed to create a temporary file named (%ws) to transfer object (%ws), hr = 0x%lx\n",(PWSTR)strOriginalFileName.GetString(), szSelection, hr);
    }
}

Por último, el objeto IStream de origen se copia en el destino del equipo.

if (SUCCEEDED(hr))
{
    DWORD cbTotalBytesWritten = 0;

    // Since we have IStream-compatible interfaces, call our helper function
    // that copies the contents of a source stream into a destination stream.
    hr = StreamCopy(pFinalFileStream,       // Destination (The Final File to transfer to)
                    pObjectDataStream,      // Source (The Object's data to transfer from)
                    cbOptimalTransferSize,  // The driver specified optimal transfer buffer size
                    &cbTotalBytesWritten);  // The total number of bytes transferred from device to the finished file
    if (FAILED(hr))
    {
        printf("! Failed to transfer object from device, hr = 0x%lx\n",hr);
    }
    else
    {
        printf("* Transferred object '%ws' to '%ws'.\n", szSelection, (PWSTR)strOriginalFileName.GetString());
    }
}

IPortableDevice (interfaz)

IPortableDeviceContent (Interfaz)

IPortableDeviceValues (Interfaz)

Guía de programación