Microsoft KB Archive/276556

= How To Launch Device-Side Applications at System Events =

Article ID: 276556

Article Last Modified on 7/2/2004

-

APPLIES TO


 * Microsoft eMbedded Visual Basic 3.0

-



This article was previously published under Q276556



SUMMARY
The CeRunAppAtEvent API function allows developers to launch an application when a specified event occurs. Such events include the following:


 * When a PC Card device is changed.
 * When a full device data restore completes.
 * When an RS232 connection is made.
 * When data synchronization finishes.
 * When the system time is changed.
 * When the time zone is changed.
 * When the device wakes up.

Use the CeRunAppAtEvent function sparingly, however, because the automatic starting of an application can confuse the user and cause low-memory conditions on a computer with restricted memory. Ideally, the application should be small and non-intrusive.

NOTE: This information applies to the version of the operating system as provided by Microsoft. Actual implementation is determined by the original equipment manufacturer (OEM) and some devices may not support this function.



MORE INFORMATION
The following steps illustrate how to set an application to launch when the synchronization process has completed:  Start a new Windows CE Project in eMbedded Visual Basic. Form1 is created by default. Place two CommandButtons and a Label control on Form1.  Paste the following code into Form1: Option Explicit

Const NOTIFICATION_EVENT_NONE = 0 Const NOTIFICATION_EVENT_TIME_CHANGE = 1 Const NOTIFICATION_EVENT_SYNC_END = 2 Const NOTIFICATION_EVENT_DEVICE_CHANGE = 7 Const NOTIFICATION_EVENT_RS232_DETECTED = 9 Const NOTIFICATION_EVENT_RESTORE_END = 10 Const NOTIFICATION_EVENT_WAKEUP = 11          'PocketPC only Const NOTIFICATION_EVENT_TZ_CHANGE = 12       'PocketPC only

Const APP_RUN_AT_TIME = &quot;AppRunAtTime&quot;

' * Prefix of the command line when the user requests to run the application ' * that &quot;owns&quot; a notification. It is followed by a space, and the ' * &quot;stringized&quot; version of the notification handle.

Const APP_RUN_TO_HANDLE_NOTIFICATION = &quot;AppRunToHandleNotification&quot;

'The application is started with a system-defined command 'line. If there was already an instance of the application 'running, the new instance must send a private message to 'the existing instance and then shut down. The command line, 'which corresponds to the registered event, can be one of 'the following string constants.

Const APP_RUN_AFTER_TIME_CHANGE = &quot;AppRunAfterTimeChange&quot; Const APP_RUN_AFTER_SYNC = &quot;AppRunAfterSync&quot; Const APP_RUN_AT_DEVICE_CHANGE = &quot;AppRunDeviceChange&quot; Const APP_RUN_AT_RS232_DETECT = &quot;AppRunAtRs232Detect&quot; Const APP_RUN_AFTER_RESTORE = &quot;AppRunAfterRestore&quot;

Declare Function CeRunAppAtEvent Lib &quot;Coredll&quot; ( _   ByVal pwszAppName As String, _    ByVal lWhichEvent As Long) As Boolean Private Sub Form_Load Command1.Move 100, 100, 1335, 495 Command2.Move 100, 700, 1335, 495 Label1.Move 100, 1300, 1335, 594 Command1.Caption = &quot;Launch Calc at Sync End&quot; Command2.Caption = &quot;Clear setting&quot; Label1.Caption = &quot;&quot; End Sub

Private Sub Command1_Click Dim bRet As Boolean bRet = False bRet = CeRunAppAtEvent(&quot;\Windows\calc.exe&quot;, _                           NOTIFICATION_EVENT_SYNC_END) If bRet Then Label1.Caption = &quot;Success&quot; Else Label1.Caption = &quot;Failure&quot; End If End Sub

Private Sub Command2_Click Dim bRet As Boolean bRet = False bRet = CeRunAppAtEvent(&quot;\Windows\calc.exe&quot;, _                           NOTIFICATION_EVENT_NONE) If bRet Then Label1.Caption = &quot;Success&quot; Else Label1.Caption = &quot;Failure&quot; End If End Sub  Run the project on the remote device, and then click Command1. Re-sync the device through ActiveSync, and note that Calc.exe launches.

Note The NOTIFICATION_EVENT_SYNC_END event is only triggered after a manual sync. For example, the NOTIFICATION_EVENT_SYNC_END event is only triggered if the user clicks the Sync button in ActiveSync on the desktop. If the mobile device syncs automatically when it is connected to the PC, this event is not triggered at the end of the sync.

