How To Launch Device-Side Applications at System Events (276556)



The information in this article 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:
  1. Start a new Windows CE Project in eMbedded Visual Basic. Form1 is created by default.
  2. Place two CommandButtons and a Label control on Form1.
  3. 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 = "AppRunAtTime"
    
    ' * Prefix of the command line when the user requests to run the application
    ' * that "owns" a notification.  It is followed by a space, and the
    ' * "stringized" version of the notification handle.
    
    Const APP_RUN_TO_HANDLE_NOTIFICATION = "AppRunToHandleNotification"
    
    '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 = "AppRunAfterTimeChange"
    Const APP_RUN_AFTER_SYNC = "AppRunAfterSync"
    Const APP_RUN_AT_DEVICE_CHANGE = "AppRunDeviceChange"
    Const APP_RUN_AT_RS232_DETECT = "AppRunAtRs232Detect"
    Const APP_RUN_AFTER_RESTORE = "AppRunAfterRestore"
    
    Declare Function CeRunAppAtEvent Lib "Coredll" ( _
        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 = "Launch Calc at Sync End"
        Command2.Caption = "Clear setting"
        Label1.Caption = ""
    End Sub
    
    Private Sub Command1_Click()
        Dim bRet As Boolean
        bRet = False
        bRet = CeRunAppAtEvent("\Windows\calc.exe", _
                                NOTIFICATION_EVENT_SYNC_END)
        If bRet Then
            Label1.Caption = "Success"
        Else
            Label1.Caption = "Failure"
        End If
    End Sub
    
    Private Sub Command2_Click()
        Dim bRet As Boolean
        bRet = False
        bRet = CeRunAppAtEvent("\Windows\calc.exe", _
                                NOTIFICATION_EVENT_NONE)
        If bRet Then
            Label1.Caption = "Success"
        Else
            Label1.Caption = "Failure"
        End If
    End Sub
    					
  4. Run the project on the remote device, and then click Command1.
  5. 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.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

217159 INF: Automatically Starting an Application Upon Flash Card Insertion

Notify.h header file

Note that the following constants are defined in the header file, but they are not supported:
NOTIFICATION_EVENT_IR_DISCOVERED 
NOTIFICATION_EVENT_NET_CONNECT 
NOTIFICATION_EVENT_NET_DISCONNECT 
NOTIFICATION_EVENT_OFF_AC_POWER 
NOTIFICATION_EVENT_ON_AC_POWER
				

Modification Type:MinorLast Reviewed:7/2/2004
Keywords:kbhowto KB276556