Windows Mobile 下显示 Flash Card 容量和序列号的代码[转载]

来源:互联网 发布:nginx last break 编辑:程序博客网 时间:2024/05/16 11:00

 

 Windows Mobile 下显示 Flash Card 容量和序列号的代码[转载]

版权所有为 王军杰

原文地址

http://blog.csdn.net/wang_junjie/archive/2007/11/04/1865569.aspx

  1. #include <projects.h> 
  2. #include <winioctl.h> 
  3. // Include library Note_Prj.lib 
  4. #pragma comment (lib, "Note_Prj.lib") 
  5. //-------------------------------------------------------------------------­----- 
  6. // 
  7. // Static data members 
  8. // 
  9. //-------------------------------------------------------------------------­----- 
  10. TCHAR CFlashCard::m_CardPath[CFlashCard::MAX_CARD_PATH]; 
  11. const UINT CFlashCard::MINIMUM_CARD_SIZE = 63 * (1024 * 1024); 
  12. //-------------------------------------------------------------------------­----- 
  13. // 
  14. // Used to see if a flash card is inserted into a socket.  Return true 
  15. if card 
  16. // is found, false otherwise.  Also, if found, sets card's path 
  17. // 
  18. //-------------------------------------------------------------------------­----- 
  19. BOOL CFlashCard::isCardAvailable() 
  20.         BOOL res = TRUE; 
  21.         HANDLE hFlashCard = NULL; 
  22.         WIN32_FIND_DATA find; 
  23.         BOOL loop = TRUE; 
  24.         UINT flashCardCount = 0; 
  25.         ULARGE_INTEGER freeBytesAvailableToCaller; 
  26.         ULARGE_INTEGER totalNumberOfBytes; 
  27.         ULARGE_INTEGER totalNumberOfFreeBytes; 
  28.         // Look for the SD card. 
  29.         memset(&find, 0, sizeof(WIN32_FIND_DATA)); 
  30.         hFlashCard = ::FindFirstFlashCard(&find); 
  31.         if(INVALID_HANDLE_VALUE != hFlashCard) 
  32.         { 
  33.                 // We must enumerate the flash cards, since the dumb 
  34.                 // iPAQ file store is defined as a flash card 
  35.                 while(loop) 
  36.                 { 
  37.                         // Only look at the flash card if it is a directory and temporary 
  38.                         if(((find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 
  39. FILE_ATTRIBUTE_DIRECTORY) && 
  40.                 ((find.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) == 
  41. FILE_ATTRIBUTE_TEMPORARY)) 
  42.                         { 
  43.                                 // NOTE: Even though we specify temporary, the local storage will 
  44. also be 
  45.                                 // found, such as "iPaq File Store" for ipaqs and "Built-in Storage" for 
  46.                                 // Dells.  So, instead of filtering on a name, we will filter on a 
  47.                                 // volumn size.  The storage must be at least MIN_FLASH_CARD_SIZE 
  48. for us 
  49.                                 // to consider it to be a storage card 
  50.                                 res = ::GetDiskFreeSpaceEx(find.cFileName, &freeBytesAvailableToCaller, 
  51.                                                            &totalNumberOfBytes, 
  52. &totalNumberOfFreeBytes); 
  53.                                 // Only count the card if it is the correct size 
  54.                                 if((res == TRUE) && (totalNumberOfBytes.QuadPart >= MINIMUM_CARD_SIZE)) 
  55.                                 { 
  56.                                         // Save the name of the flash card 
  57.                                         _tcsncpy(m_CardPath, find.cFileName, MAX_CARD_PATH); 
  58.                                         flashCardCount++; 
  59.                                 } 
  60.                         } 
  61.                         // Next flash card 
  62.                         loop = ::FindNextFlashCard(hFlashCard, &find); 
  63.                 } 
  64.                 ::FindClose (hFlashCard); 
  65.                 // If no flash cards were found in the enumeration, then leave 
  66.                 if(flashCardCount == 0) 
  67.                 { 
  68.                         res = FALSE; 
  69.                 } 
  70.         } 
  71.         else 
  72.         { 
  73.                 // No flash cards found 
  74.                 _stprintf(m_CardPath, _T("ERR: %d"), ::GetLastError()); 
  75.                 res = FALSE; 
  76.         } 
  77.         return res; 
  78. //-------------------------------------------------------------------------­----- 
  79. // 
  80. // Get the flash card serial number and save in passed in buffer. 
  81. // 
  82. //-------------------------------------------------------------------------­----- 
  83. BOOL CFlashCard::getSerialNumber(TCHAR* buf, UINT bufLength) 
  84.         // These values are only defined in Platform Builder, so we have to 
  85.         // redefine them here 
  86.         #define IOCTL_DISK_BASE           FILE_DEVICE_DISK 
  87.         #define IOCTL_DISK_GET_STORAGEID  CTL_CODE(IOCTL_DISK_BASE, 0x709, / 
  88.                                                                                            METHOD_BUFFERED, FILE_ANY_ACCESS) 
  89.         #define MANUFACTUREID_INVALID     0x01 
  90.         #define SERIALNUM_INVALID         0x02 
  91.         // This structure is only defined in Platform Builder, so we have to 
  92.         // redefine it here 
  93.         typedef struct _STORAGE_IDENTIFICATION 
  94.         { 
  95.                 DWORD    dwSize; 
  96.                 DWORD    dwFlags; 
  97.                 DWORD    dwManufactureIDOffest; 
  98.                 DWORD    dwSerialNumOffset; 
  99.         } STORAGE_IDENTIFICATION, *PSTORAGE_IDENTIFICATION; 
  100.         const UINT BBUF_LENGTH = 256; 
  101.         const UINT STR_LENGTH = 256; 
  102.         const UINT SERIAL_NUMBER_LENGTH = 8; 
  103.         BOOL res = TRUE; 
  104.         byte bbuf[BBUF_LENGTH]; 
  105.         TCHAR str[STR_LENGTH]; 
  106.         STORAGE_IDENTIFICATION* si; 
  107.         HANDLE hCard = NULL; 
  108.         ULONG dwNumReturned = 0; 
  109.         ULONG err = 0; 
  110.         // Make sure a card is inserted and found before attempting to read 
  111.         // the serial number 
  112.         if(isCardAvailable() == TRUE) 
  113.         { 
  114.                 // Clear the buffer 
  115.                 memset(bbuf, 0, BBUF_LENGTH); 
  116.                 // Clear the buffer 
  117.                 memset(buf, 0, bufLength * sizeof(TCHAR)); 
  118.                 // Set a STORAGE_IDENTIFICATION pointer to the buffer (overlaying the 
  119. structure) 
  120.                 si = (STORAGE_IDENTIFICATION*)bbuf; 
  121.                 // Create the volume name of the flash card 
  122.                 _stprintf(str, _T("//%s//Vol:"), m_CardPath); 
  123.                 // Get a handle to the flash card (drive) volume 
  124.                 hCard = ::CreateFile(str, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); 
  125.                 // Set the size element of the STORAGE_IDENTIFICATION structure 
  126.                 si->dwSize = BBUF_LENGTH; 
  127.                 // Fill the STORAGE_IDENTIFICATION structure with the flash card info 
  128.                 if(::DeviceIoControl(hCard, IOCTL_DISK_GET_STORAGEID, (LPVOID)NULL, 0, 
  129.                                                      si, BBUF_LENGTH, &dwNumReturned, NULL) == FALSE) 
  130.                 { 
  131.                         err = ::GetLastError(); 
  132.                         res = FALSE; 
  133.                 } 
  134.                 // Close the handle 
  135.                 ::CloseHandle (hCard); 
  136.                 // See if the serial number is valid.  If not, return false, otherwise 
  137. get 
  138.                 // the serial number 
  139.                 if((si->dwFlags & SERIALNUM_INVALID) != SERIALNUM_INVALID) 
  140.                 { 
  141.                         // Get a char pointer to the serial number in the byte array 
  142.                         CHAR* sn = (CHAR*)((UINT)si + si->dwSerialNumOffset); 
  143.                         // Covert the CHAR serial number to a TCHAR string 
  144.                         mbstowcs(buf, sn, SERIAL_NUMBER_LENGTH); 
  145.                         buf[SERIAL_NUMBER_LENGTH] = 0; 
  146.                 } 
  147.                 else 
  148.                 { 
  149.                         // Serial number is not available 
  150.                         _tcscpy(buf, _T("! NONE !")); 
  151.                         res = FALSE; 
  152.                 } 
  153.         } 
  154.         else 
  155.         { 
  156.                 // No card 
  157.                 _tcscpy(buf, _T("NO CARD")); 
  158.                 res = FALSE; 
  159.         } 
  160.         return res;