I’ve recently had need to ensure that a CF app of mine is automatically launched on rebooting a Windows Mobile device. Googling for help on this didn’t give me quite enough help, so I’m making sure I get this written down for future reference. Chris Tacke touched on the subject, and MSDN had a little information.
When the PocketPC / Windows Mobile OS starts, it looks in the HKEY_LOCAL_MACHINE\init registry key for a list of processes which it should start. The entries in this key are arranged like this:
| Entry | Value | Type |
| Launchnn | <path to process exe> | REG_SZ |
| Dependnn | hex:xx,yy[,xx,yy] | REG_BINARY (yy is most signficant) |
in this table nn is a value which determines the order in which processes are started. The Dependnn entry is optional, and is used to specify any processes upon which the entry has dependencies. For example:
[HKEY_LOCAL_MACHINE\Init]
"Launch10"="shell.exe"
"Launch20"="device.dll"
"Launch30"="gwes.dll"
"Depend30"=hex:14,00
"Launch50"="taskman.exe"
"Depend50"=hex:14,00, 1e,00
Here the first entry (shell.exe) does not have any dependencies, whereas gwes.dll has a dependency on device.dll (0x14 = 20) and taskman.exe has a dependency on both device.dll and gwes.dll (0x1e = 30).
A typical CF app should ideally have dependencies on both shell32.exe (Launch50 / 0x32) and services.exe (Launch60 / 0x3C). Therefore to ensure that your app launches only after these two processes have started, you would add a Launchnn and Dependnn entry with a nn value higher than both of these processes.
Note: there is a restriction on the number of Launchnn / Dependnn entries in the registry. No more than 32 applications can be specified.
As an example, here are the registry entries I have added as part of my CAB installer package:
[HKEY_LOCAL_MACHINE\Init]
"Launch95"="%InstallDir%\MgdSvcsLauncher.exe"
"Depend95"=hex:32,00, 3C,00
Note that I’ve included the %InstallDir% macro as part of the path. This ensures that the correct path is inserted into the registry regardless of which folder the app is installed to.
Dependency Notification
If we have a process listed in the above registry key with dependencies, how do we know when those dependencies have started? This is straightforward, the OS won’t launch our process until all our dependencies have notified the OS that they have started.
But how can we notify the OS that our application has started, so that any processes dependant on our application may start? Here we have to use a native API call.
When launched our process will receive an argument value corresponding to the nn value defined in our registry entry (Launchnn). Next we need to import the native API we use to notify the OS:
[DllImport("coredll")]
public static extern void SignalStarted(uint dword);
And lastly we just need to call SIgnalStarted with the nn value passed to us as an argument when we are sure that any dependant processes can use our application if required. This isn’t always appropriate to our code, and won’t affect anything if it’s not called, however any dependant processes will not be automatically launched until SignalStarted is called.