次の方法で共有


接続の確立

ユーザーがデバイスを選択すると、ChooseDevice 関数が IPortableDevice::Open メソッドを呼び出して、アプリケーションとデバイス間の接続を確立します。 IPortableDevice::Open メソッドは、次の 2 つの引数を受け取ります。

// 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);
}

Windows 7 の場合、 IPortableDevice ではCoCreateInstance 用に 2 つの CLSID がサポートされています。 CLSID_PortableDevice は、フリー スレッド マーシャラーを集計しない IPortableDevice ポインターを返します。 CLSID_PortableDeviceFTM は、フリー スレッド マーシャラーを集計する IPortableDevice ポインターを返す新しい CLSID です。 それ以外の場合は、両方のポインターで同じ機能がサポートされます。

シングル スレッド アパートメントに存在するアプリケーションでは 、インターフェイス ポインターマーシャリングのオーバーヘッドを排除するため、CLSID_PortableDeviceFTMを使用する必要があります。 CLSID_PortableDevice は、レガシ アプリケーションでは引き続きサポートされています。

デバイスの選択