QA: How to implement autorun from a storage card?

Joao Paulo Figueira (joao.fig@mail.telepac.pt), June 02, 2003.

Question

I want to start an application when the user inserts or removes a storage card. How do I implement such functionality?

Answer

You can execute an application when a storage card is inserted or removed from your device. This may allow your users to install applications, perform backups, or other action you like. The protocol is really simple:

Here is a skeleton application. You can create it using eVC's Wizard, choosing the 'WCE Pocket PC 2002 Application'.

TCHAR szCFPath [MAX_PATH+1]; // Compact Flash path // OnCardInsert // // The compact flash card has been inserted // void OnCardInsert() { // // Get the path from where we are starting up // if(!SHGetAutoRunPath(szCFPath)) { return; } } // OnCardRemove // // The compact flash card has been removed // void OnCardRemove() { } // WinMain // // Main entry point // int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { if(lstrcmpi(lpCmdLine, _T("install")) == 0) { // // Card has been inserted // OnCardInsert(); } else if(lstrcmpi(lpCmdLine, _T("uninstall")) == 0) { // // Card has been removed // OnCardRemove(); } return 0; }

Note that your application does not know where it is starting. You cannot infer that the startup path will be the one you are expecting.

Related resources: