列舉裝置

[與此頁面相關聯的功能Windows 媒體播放機 SDK是舊版功能。 MediaPlayer已取代它。 MediaPlayer已針對Windows 10和Windows 11進行優化。 Microsoft 強烈建議新程式碼盡可能使用MediaPlayer,而不是Windows 媒體播放機 SDK。 Microsoft 建議使用舊版 API 的現有程式碼盡可能重寫為使用新的 API。

Windows 媒體播放機表示使用IWMPSyncDevice介面的可攜式裝置。 下列範例程式碼顯示一個函式,該函式會建立 IWMPSyncDevice的指標陣列。 陣列中的每個指標都代表Windows 媒體播放機已儲存資訊的裝置。 裝置不需要連線到電腦,也不需要與目前Windows 媒體播放機實例建立合作關係。

每當收到 DeviceConnect 事件或 DeviceDisconnect 事件時,您應該列舉裝置。

下列函式會列舉裝置。 bConnectedOnly參數會指定是否只列舉目前連線到使用者電腦的裝置。

STDMETHODIMP CMainDlg::EnumDevices(BOOL bConnectedOnly)
{
    HRESULT hr = S_OK;
    CComPtr<IWMPSyncServices>  spSyncServices;
    long cDeviceArray = 0;  // Size to make the array
    long cAllDevices = 0;  // Count of all devices
    long cDeviceIndex = 0; // Index for adding devices to array.
    CComPtr<IWMPSyncDevice> spTempDevice;
    VARIANT_BOOL vbIsConnected = VARIANT_FALSE;

    // Delete any existing array.
    FreeDeviceArray();

    // Retrieve a pointer to IWMPSyncServices.
    hr = m_spPlayer->QueryInterface(&spSyncServices);

    if(SUCCEEDED(hr) && spSyncServices.p)
    {  
        hr = spSyncServices->get_deviceCount(&cAllDevices);
    }

    if(SUCCEEDED(hr) && cAllDevices > 0)
    {
        if(bConnectedOnly)
        {
            // Count the number of connected devices.
            for(long i = 0; i < cAllDevices; i++)
            {     
                spTempDevice.Release();
                hr = spSyncServices->getDevice(i, &spTempDevice);

                if(SUCCEEDED(hr) && spTempDevice.p)
                {
                    spTempDevice->get_connected(&vbIsConnected);

                    if(vbIsConnected == VARIANT_TRUE)
                    {
                        // Count only the connected devices.
                        cDeviceArray++;
                    }
                }
            }
        }
        else
        {
            cDeviceArray = cAllDevices;
        }

        if(cDeviceArray == 0)
        {
            return hr;
        }

        // Cache the device count.
        m_cDevices = cDeviceArray;

        // Create a new device array.
        m_ppWMPDevices = new IWMPSyncDevice*[cDeviceArray];
        if(!m_ppWMPDevices)
        {
            return E_OUTOFMEMORY;
        }
        
        ZeroMemory(m_ppWMPDevices, sizeof (IWMPSyncDevice*) * cDeviceArray);

        // Fill the array.
        for(long i = 0; i < cAllDevices; i++)
        {
            spTempDevice.Release();
            hr = spSyncServices->getDevice(i, &spTempDevice);
            if(SUCCEEDED(hr) && spTempDevice.p)
            {
                spTempDevice->get_connected(&vbIsConnected);

                if( (bConnectedOnly && vbIsConnected == VARIANT_TRUE) ||
                     !bConnectedOnly )
                {
                    // Add the device to the custom array.
                    spTempDevice.CopyTo(&m_ppWMPDevices[cDeviceIndex++]);
                }
            }
        }
    }

    return hr;
}

您可以使用類似的程式碼來擷取其他這類裝置清單。 例如,您可以使用 IWMPSyncDevice::get_status 來建立合作關係所在的裝置陣列。

IWMPEvents2::D eviceConnect

IWMPEvents2::D eviceDisconnect

IWMPSyncDevice 介面

IWMPSyncDevice::get_connected

IWMPSyncServices 介面

使用可攜式裝置