Share via


Establecer una conexión

Una vez que el usuario selecciona un dispositivo, la función ChooseDevice, a su vez, llama al método IPortableDevice::Open para establecer una conexión entre la aplicación y el dispositivo. El método IPortableDevice::Open toma dos argumentos:

// CoCreate the IPortableDevice interface and call Open() with
// the chosen PnPDeviceID string.
hr = CoCreateInstance(CLSID_PortableDeviceFTM,
                      NULL,
                      CLSCTX_INPROC_SERVER,
                      IID_PPV_ARGS(ppDevice));
if (SUCCEEDED(hr))
{
    hr = (*ppDevice)->Open(pPnpDeviceIDs[uiCurrentDevice], pClientInformation);
    if (FAILED(hr))
    {
        if (hr == E_ACCESSDENIED)
        {
            printf("Failed to Open the device for Read Write access, will open it for Read-only access instead\n");
            pClientInformation->SetUnsignedIntegerValue(WPD_CLIENT_DESIRED_ACCESS, GENERIC_READ);
            hr = (*ppDevice)->Open(pPnpDeviceIDs[uiCurrentDevice], pClientInformation);
            if (FAILED(hr))
            {
                printf("! Failed to Open the device, hr = 0x%lx\n",hr);
                // Release the IPortableDevice interface, because we cannot proceed
                // with an unopen device.
                (*ppDevice)->Release();
                *ppDevice = NULL;
            }
        }
        else
        {
            printf("! Failed to Open the device, hr = 0x%lx\n",hr);
            // Release the IPortableDevice interface, because we cannot proceed
            // with an unopen device.
            (*ppDevice)->Release();
            *ppDevice = NULL;
        }
    }
}
else
{
    printf("! Failed to CoCreateInstance CLSID_PortableDeviceFTM, hr = 0x%lx\n",hr);
}

Para Windows 7, IPortableDevice admite dos CLSID para CoCreateInstance. CLSID_PortableDevice devuelve un puntero IPortableDevice que no agrega el serializador sin subprocesos; CLSID_PortableDeviceFTM es un nuevo CLSID que devuelve un puntero IPortableDevice que agrega el serializador de subprocesos libre. Ambos punteros admiten la misma funcionalidad de lo contrario.

Las aplicaciones que residen en Apartamentos de un solo subproceso deben usar CLSID_PortableDeviceFTM , ya que esto elimina la sobrecarga de serialización de punteros de interfaz. CLSID_PortableDevice sigue siendo compatible con las aplicaciones heredadas.

Selección de un dispositivo