Compact Flash and Other Storage Devices

< BACK  NEXT >
[oR]

Storage devices extend the amount of data stored in a Windows CE device from the maximum allowed in the Object Store. The most common type of storage device is Compact Flash (CF) and ATA cards, although CDROM, DVD, FAT, and other storage devices are becoming more widespread.

Most storage devices are removable, so knowing when the user puts in or takes out a device can be important. In Windows CE a WM_DEVICECHANGE message is sent to the main application window when a removable storage device is added or removed. You need to include the file dbt.h when using this message. You can respond to this message using the following code in the window's message-processing function.

 case WM_DEVICECHANGE:   switch (wParam)   {   case DBT_DEVICEARRIVAL:   case DBT_DEVICEREMOVECOMPLETE:     Listing3_5(wParam,       (DEV_BROADCAST_HDR*)lParam); break; 

The wParam parameter has the value DBT_DEVICEARRIVAL when a device is inserted, and DBT_DEVICEREMOVECOMPLETE when the device is removed (Listing 3.5). You should note that the WM_DEVICECHANGE message is also sent when any PCMCIA (such as modem or network card) or other removable device is inserted or removed. Your application will determine whether a storage device caused the message to be sent using the techniques shown in Listing 3.6.

Listing 3.5 Response to insertion or removal of a storage card (called in response to WM_DEVICECHANGE message)
 void Listing3_5(WORD wParam, DEV_BROADCAST_HDR* dbt) {   // Must include dbt.h   if(wParam == DBT_DEVICEARRIVAL)      cout   _T("Device inserted")   endl;   else if(wParam == DBT_DEVICEREMOVECOMPLETE)      cout   _T("Device removed")   endl; } 

A special situation occurs when the Windows CE device is turned on. Windows CE simulates a removal and insertion of the device before applications are allowed to access the device. This means your application will receive two WM_DEVICECHANGE messages (a DBT_DEVICEREMOVECOMPLETE and DBT_ DEVICEARRIVAL) for each removable device when the Windows CE device is turned on.

Auto-Run Applications on Compact Flash Cards

Starting with Windows CE 3.0 it is possible to have an application run from a Compact Flash memory card when it is inserted into a device. This allows an application to auto-install from a Compact Flash card.

To set an application to be auto-run, you must place the application in aspecific folder for the CPU targeted by your application. The folder name isbased on the CPU number returned in the dwProcessorType member of theSYSTEM_INFO structure returned from calling GetSystemInfo. Table 3.7 shows the possible values and their associated constants.

Table 3.7. Processor values and associated constants
Constant Value
PROCESSOR_MIPS_R4000 4000
PROCESSOR_HITACHI_SH3 10003
PROCESSOR_HITACHI_SH3E 10004
PROCESSOR_HITACHI_SH4 10005
PROCESSOR_MOTOROLA_821 821
PROCESSOR_SHx_SH3 103
PROCESSOR_SHx_SH4 104
PROCESSOR_STRONGARM 2577
PROCESSOR_ARM720 1824
PROCESSOR_ARM820 2080
PROCESSOR_ARM920 2336
PROCESSOR_ARM_7TDMI 70001

Thus, if you want your application to auto-run and the application is compiled for MIPS, you should rename your application to autorun.exe and place it in a folder called \4000, for example, \4000\autorun.exe.

If your application is compiled for CEF (Common Executable Format), you should place the autorun.exe file in a folder called \0, for example, \0\autorun.exe.

The application autorun.exe is passed the command line parameter "install" when a Compact Flash card is inserted, and with the command line parameter "uninstall" when the card is removed. This allows your autorun.exe application to uninstall itself when the card is removed. The autorun.exe application typically has a simple WinMain that tests for the two valid command line values:

 int WINAPI WinMain(HINSTANCE hInst,      HINSTANCE hInstPrev, LPTSTR lpszCmdLine,      int nCmdShow) {   if (lstrcmpi(lpszCmdLine, _T("install") == 0)   {      OnCardInsert();       // function installs   }   else   {      OnCardEject();        // function uninstalls   }   return 0; } 

Enumerating Compact Flash Cards

The code in Listing 3.4 in the section "Traversing Directory Trees" showed how to search for files and how to recognize a Compact Flash card from the related directory's attributes. In Windows CE 3.0 the FindFirstFlashCard and FindNextFlashCard functions can be used to enumerate all flash cards installed on a device, and this is much easier. The functions operate in very much the same way as FindFirstFile and FindNextFile. Listing 3.6 lists all the Compact Flash cards present on the device. You need to include projects.h into your source files and Note_Prj.Lib into the project.

Listing 3.6 Enumerates all Compact Flash cards
 #include <projects.h> // link with NOTE_PRJ.LIB void Listing3_6() {   HANDLE hCF;   WIN32_FIND_DATA fndMountable;   hCF = FindFirstFlashCard(&fndMountable);   if(hCF == INVALID_HANDLE_VALUE)      cout   _T("No CF Cards")   endl;   else   {     do     {       cout    _T("CF Card: ")               fndMountable.cFileName   endl;     } while(FindNextFlashCard(&fndMountable,          &fndMountable));     FindClose(hCF);   } } 

The function FindFirstFlashCard takes a single argument, a pointer to a WIN32_FIND_DATA structure, and returns a search handle, stored in hCF. The search handle has a value of INVALID_HANDLE_VALUE if the search fails (for example, if there are no Compact Flash cards). The code in Listing 3.6 lists the folder name associated with the Compact Flash card (for example, "Storage Card"), and then calls FindNextFlashCard. The function is passed the search handle, hCF, and a pointer to a WIN32_FIND_DATA structure. The function returns FALSE when all Compact Flash cards have been listed. The search handle should be passed to FindClose when the list is complete.


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net