Partager via


Enumerating and Querying Storages

banner art

Access to the digital media content on a portable device is provided through the interfaces IWMDMEnumStorage and IWMDMStorage. By calling IWMDMDevice::EnumStorage, a pointer to the IWMDMEnumStorage interface can be obtained. This interface is used to move sequentially through the storage media on a device. The method IWMDMEnumStorage::Next gets an instance of the IWMDMStorage interface, which is associated with a storage medium on the device. IWDMStorage can represent an entire storage medium or be further enumerated to represent each object, such as a folder or file, on that medium. The complete organization of a hierarchical storage medium can be traversed using this interface.

The following example code illustrates how to determine what storages are available on a device and how to get information about a storage and its content:

hr = pIDevice->EnumStorage(&pIEnumStorage);
if SUCCEEDED(hr)
{
    wprintf(L"Got IWMDMEnumStorage\n");
    hr = pIEnumStorage->Next(ulNumStorage, &pIStorage, &ulNumFetched);
    if SUCCEEDED(hr) 
{

wprintf(L"Got IWMDMStorage.  Number Storage Fetched = %d\n", ulNumFetched);
hr = pIStorage->GetAttributes(&dwAttribs, &pFormat);
if SUCCEEDED(hr)
{
    wprintf(L"Got Storage Attributes\n");
    if (dwAttribs & WMDM_STORAGE_ATTR_HAS_FILES)
        wprintf(L"Storage has files.\n");
}
if FAILED(hr)
    wprintf(L" Failed: GetAttributes; hr = %d\n", hr);

// Using EnumStorage(), enumerate content storages (files) on the storage.
hr = pIStorage->EnumStorage(&pIEnumStorageFile);
if SUCCEEDED(hr)
{
    wprintf(L"Got IWMDMEnumStorageFile\n");

    // Loop to enumerate content storages (files).
    for( ; ; )
    {
        hr = pIEnumStorageFile->Next(ulNumStorage , &pIStorageFile, &ulNumFetched);
        if (S_OK != hr)
            break;
        if SUCCEEDED(hr)
        {

        wprintf(L"Got IEnumStorageFile. Number Storage File Fetched = %d\n", ulNumFetched);
        hr = pIStorageFile->GetName(pwsString, iStrLength);
        if SUCCEEDED(hr)
            wprintf(L"Got Storage Name: %s\n", pwsString);

        if FAILED(hr) 
            wprintf(L" Failed: Get Storage Name; hr = %d\n", hr);

wprintf(L"Reference Count IStorageFile: %d\n", pIStorageFile->Release());
        }
    }
// End of loop.

wprintf(L"Reference Count IEnumStorageFile: %d\n", pIEnumStorageFile->Release());
}
wprintf(L"Reference Count IStorage: %d\n", pIStorage->Release());
wprintf(L"Reference Count IEnumStorage: %d\n", pIEnumStorage->Release());

See Also