Share via


Establishing a Connection

banner art

Previous Next

Establishing a Connection

Once the user selects a device, the ChooseDevice function, in turn, calls the IPortableDevice::Open method to establish a connection between the application and the device. The IPortableDevice::Open method takes two arguments:

  • // CoCreate the IPortableDevice interface and call Open() with
    // the chosen PnPDeviceID string.
    hr = CoCreateInstance(CLSID_PortableDevice,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_IPortableDevice,
                          (VOID**) ppDevice);
    if (SUCCEEDED(hr))
    {
        if (ppDevice != NULL)
        {
            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
        {
            hr = E_UNEXPECTED;
            printf("! Failed to Open the device because we were returned a NULL IPortableDevice interface pointer, hr = 0x%lx\n",hr);
        }
    }
    else
    {
        printf("! Failed to CoCreateInstance CLSID_PortableDevice, hr = 0x%lx\n",hr);
    }
    

}

See Also

Previous Next