Using the PocketPC Event Notification API

The PocketPC API provides a mechanism for getting notified when the iPAQ is turned on.  Note that technically nothing at all happens to running programs when an iPAQ is turned off.  It's only when the iPAQ is turned back on that the operating system unloads and reloads drivers and the Bluetooth stack itself.  It can also tell you when this is happening.

There are three steps in using the event notification system, as outlined below:

1. Register for the NOTIFICATION_EVENT_WAKEUP event.  You don't actually get this event sent to your program.  The way it works is the operating system launches the .EXE you specify in the registration call.

CE_NOTIFICATION_TRIGGER nt;

memset(&nt, 0, sizeof (CE_NOTIFICATION_TRIGGER));
nt.dwSize = sizeof (CE_NOTIFICATION_TRIGGER);
nt.dwType = CNT_EVENT;
nt.dwEvent = NOTIFICATION_EVENT_WAKEUP;
nt.lpszApplication = _T("\\Windows\\MyPowerMon.exe");
nt.lpszArguments = _T("");

m_hWakeupEvent = CeSetUserNotificationEx (0, &nt, NULL);

2. Write the .EXE that will be launched upon power-on.  You can write this program to use FindWindow (or your own custom logic) to locate your original running program and send it a user-defined message.  In the case of BTAccess54 you could send the same BTMSG_STACK_RELOADED message which was the message we used for the standard BTAccess library.  That way your application could be set up to respond to this event no matter which version of BTAccess you used.  This app should terminate itself after doing this small job. 

3. Unregister for the wakeup event back in your original program when it terminates.  There is a safer way to do this instead of using the CeSetUserNotification API again:

CeRunAppAtEvent (_T("\\Windows\\MyPowerMon.exe"),NOTIFICATION_EVENT_NONE);