我在便携设备上写程序,需要查询设备上的文件系统目录结构,比如,我要知道设备的外部存储卡的目录,还要知道设备的flash分区的目录。这样,我要保存文件的时候,尽量先往存储卡上写,如果没有插卡,再往flash分区写。
首先查找MSDN,找到了FindFirstFlashCard这组API可用。
很高兴,在WM的SDK上编译链接过了,在WM设备上使用也很成功。
我又换了一台WINCE的设备,用CE5的SDK编译我的程序,编译不过,没有找到FindFirstFlashCard函数。
无奈,查找资料,又找到了FindFirstStore、FindFirstPartition这组API可以在CE上使用。
但是,FindFirstStore、FindFirstPartition在WM SDK中只有声明,没有定义,链接不过。
再无奈,只好在代码里面用宏定义来区分WINCE和WM,分别使用这两组API。
再找找有没有其他的办法。
试验代码如下:
//FindFirstFlashCard
{ 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 0; hFlashCard = FindFirstFlashCard (&lpwfdFlashCardTmp [0]); if (hFlashCard == INVALID_HANDLE_VALUE) { // Free the memory. LocalFree (lpwfdFlashCardTmp); return 0; } 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 0; } // Assign lpwfdFlashCardTmp to lpwfdFlashCard. for (index=0; index < iNumOfFlashCard; ++index) { lpwfdFlashCard [index] = lpwfdFlashCardTmp [index]; } // Free the memory. LocalFree (lpwfdFlashCard); } LocalFree (lpwfdFlashCardTmp); }
//FindFirstStore { PARTINFO partinfo; STOREINFO storeinfo, storeinfo2; HANDLE hand1,hand2; BOOL bContinue = TRUE; int iNumOfFlashCard = 0; memset(&storeinfo2, 0, sizeof(storeinfo2)); storeinfo2.cbSize = sizeof(storeinfo2); hand1 = FindFirstStore(&storeinfo2); if (hand1 == INVALID_HANDLE_VALUE) { DWORD ret = GetLastError(); return FALSE; }
bContinue = TRUE; iNumOfFlashCard = 0; while (bContinue) { iNumOfFlashCard += 1; memset(&storeinfo, 0, sizeof(storeinfo)); storeinfo.cbSize = sizeof(storeinfo); bContinue = FindNextStore (hand1,&storeinfo); } FindCloseStore(hand1);// hand1 = OpenStore(storeinfo2.szDeviceName); if (hand1 == INVALID_HANDLE_VALUE) { DWORD ret = GetLastError(); return FALSE; }
memset(&partinfo, 0, sizeof(partinfo)); partinfo.cbSize = sizeof(partinfo); hand2 = FindFirstPartition(hand1, &partinfo); if (hand2 == INVALID_HANDLE_VALUE) { DWORD ret = GetLastError(); return FALSE; } bContinue = TRUE; iNumOfFlashCard = 0; while (bContinue) { iNumOfFlashCard += 1; memset(&partinfo, 0, sizeof(partinfo)); partinfo.cbSize = sizeof(partinfo); bContinue = FindNextPartition (hand2,&partinfo); } FindClosePartition(hand2); }