Istituzione di una connessione
Dopo aver selezionato un dispositivo, la funzione ChooseDevice, a sua volta, chiama il metodo IPortableDevice::Open per stabilire una connessione tra l'applicazione e il dispositivo. Il metodo IPortableDevice::Open accetta due argomenti:
- Puntatore a una stringa con terminazione null che specifica il nome Plug and Play del dispositivo. Questa stringa viene recuperata chiamando il metodo IPortableDeviceManager::GetDevices .
- Puntatore a un'interfaccia IPortableDeviceValues che specifica le informazioni client per l'applicazione.
// 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);
}
Per Windows 7, IPortableDevice supporta due CLSID per CoCreateInstance. CLSID_PortableDevice restituisce un puntatore IPortableDevice che non aggrega il marshalling a thread libero; CLSID_PortableDeviceFTM è un nuovo CLSID che restituisce un puntatore IPortableDevice che aggrega il marshalling a thread libero. Entrambi i puntatori supportano la stessa funzionalità in caso contrario.
Le applicazioni che vivono in appartamenti a thread singolo devono usare CLSID_PortableDeviceFTM perché elimina il sovraccarico del marshalling del puntatore dell'interfaccia. CLSID_PortableDevice è ancora supportato per le applicazioni legacy.
Argomenti correlati