Partager via


Enumerating and Querying Devices

banner art

The method IWMDeviceManager::EnumDevices retrieves a pointer to the IWMDMEnumDevice interface. This object identifies portable devices connected to the host computer provided that they are installed and registered with a Windows Media Device Manager service provider (see Registering Service Providers) and are currently connected and powered on.

The following example shows a query for IWMDMEnumDevice followed by a call to IWMDMEnumDevice::Next, which obtains a pointer to the first available device. The methods IWMDMDevice::GetManufacturer, IWMDMDevice::GetSerialNumber, IWMDMDevice::GetStatus, and IWMDMDevice::GetType, supply information from the device.

IWMDMEnumDevice* pIEnumDev;
IWMDMDevice* pIDevice;
ULONG ulNum = 1;
ULONG ulNumFetched;
LPWSTR pwsString;
PWMDMID pSerialNumberStruct;

// The following is the 8-byte message authentication code from key.c
BYTE abMac[WMDM_MAC_LENGTH];
DWORD pdwStatus;

hr = pIdvMgr->EnumDevices(&pIEnumDev); // Query for device enumerator.
if SUCCEEDED(hr)
{
    wprintf(L"Got IWMDMEnumDevice\n");
    hr = pIEnumDev->Next(ulNum, &pIDevice, &ulNumFetched); // Get device.
    
    // If no device is found, S_FALSE is returned, which is still
    // a success case.  Do not use the SUCCEEDED macro.
    if (S_OK == hr)
    {
        wprintf(L"Got IWMDMDevice\n");

        // Get manufacturer.
        hr = pIDevice->GetManufacturer(pwsString, iStrLength);
        if SUCCEEDED(hr)
            wprintf(L"Got Manufacturer: %s\n", pwsString);
        hr = hr = pIDevice->GetSerialNumber(pSerialNumberStruct,abMac);
        if SUCCEEDED(hr)
            wprintf(L"Got Serial Number Structure\n);
        hr = pIDevice->GetStatus(pdwStatus);
        if SUCCEEDED(hr)
        {
            if (WMDM_STATUS_READY & *pdwStatus)
                wprintf(L"Got status: WMDM_STATUS_READY\n");
        }
        hr = pIDevice->GetType(&dwTypeDev);
        if SUCCEEDED(hr)
        {
            wprintf(L"Got Device Type\n");

            if (WMDM_DEVICE_TYPE_PLAYBACK & dwTypeDev)
                wprintf(L"Device can playback\n");
            else wprintf(L"Device cannot playback\n");

            if (WMDM_DEVICE_TYPE_RECORD & dwTypeDev)
                wprintf(L"Device can record\n");
            else wprintf(L"Device cannot record\n");

            if (WMDM_DEVICE_TYPE_STORAGE & dwTypeDev)
                wprintf(L"Device has storage\n");
            else wprintf(L"Device does not have storage\n");

            if (WMDM_DEVICE_TYPE_SDMI & dwTypeDev)
                wprintf(L"Device can accept SDMI protected content\n"); 
            else wprintf(L"Device cannot accept SDMI protected content\n");
        }
        if FAILED(hr) wprintf(L" Failed: GetDeviceType\n");
    }
}

See Also