How to: Enumerate Storage Cards

Windows Mobile software for Pocket PCs provides the FindFirstFlashCard and FindNextFlashCard functions to allow programmatic enumeration of storage cards. FindFirstFlashCard returns a search handle that FindNextFlashCard uses. It also returns a pointer to the first storage card, if any. FindNextFlashCard returns a pointer to the next storage card and a BOOL value indicating whether the search was successful. The following code example shows how to use FindFirstFlashCard and FindNextFlashCard to locate storage cards on a Pocket PC device.

Note   Because a device can have multiple storage cards with various names, your application cannot make any assumptions about the naming of or path to a particular card.

Example

int index;
// Total number of storage cards found. 
int iNumOfFlashCard = 0;     
// Maximum number of storage cards to find.
int iMaxCards = 10;
// Whether to continue searching after finding a card.
BOOL bContinue = TRUE;       
// Root directory path of storage cards.
TCHAR szRootDirPath[MAX_PATH];   
// String for storing the storage card name with the full path. 
TCHAR szSCName[MAX_PATH];        
// Search handle for storage cards. 
HANDLE hFlashCard;         
// Structure for storing card information.
WIN32_FIND_DATA *lpwfdFlashCard;  
// Structure for storing card information temporarily.
WIN32_FIND_DATA *lpwfdFlashCardTmp; 
lpwfdFlashCardTmp = (WIN32_FIND_DATA *) LocalAlloc 
                    (LPTR, iMaxCards * sizeof (WIN32_FIND_DATA));
// Test for failed memory allocation.
if (lpwfdFlashCardTmp == NULL)
   return;
hFlashCard = FindFirstFlashCard (&lpwfdFlashCardTmp [0]);
if (hFlashCard == INVALID_HANDLE_VALUE)
{
// Free the memory.
   LocalFree (lpwfdFlashCardTmp);  
   return;
}
while (bContinue) 
{
   iNumOfFlashCard += 1;
   // Search for the next storage card.
   bContinue = FindNextFlashCard (hFlashCard, 
               &lpwfdFlashCardTmp [iNumOfFlashCard]);
}
// Close the search handle.
FindClose (hFlashCard);          
if (iNumOfFlashCard > 0)
{
   // Allocate memory for lpwfdFlashCard.
   lpwfdFlashCard = (WIN32_FIND_DATA *) LocalAlloc 
                    (LPTR, iNumOfFlashCard * sizeof (WIN32_FIND_DATA)); 
   // Test for failed memory allocation.
   if (lpwfdFlashCard == NULL)  
   {
   // Free the temp card memory.
      LocalFree (lpwfdFlashCardTmp);  
      return;
   }
   // Assign lpwfdFlashCardTmp to lpwfdFlashCard.
   for (index=0; index < iNumOfFlashCard; ++index)
   {
    lpwfdFlashCard [index] = lpwfdFlashCardTmp [index];
   }
// Free the memory.
   LocalFree (lpwfdFlashCard);  
}
LocalFree (lpwfdFlashCardTmp);  

See Also

File Management Functions

How to: List and Locate Storage Card Files

Send feedback on this topic to the authors.

© 2005 Microsoft Corporation. All rights reserved.