A few months ago I downloaded the Windows Mobile Developer Power Toys (Download details: Windows Mobile Developer Power Toys) because I had a need for CERDISP, the Windows CE Remote Display tool. At that time, I looked around at the other tools that were installed and played with RAPIStart.exe. RAPIStart.exe is a command line tool that lets you start applications on your device from your workstation using ActiveSync. A handy little tool, so I made a note to try to reproduce it and write a little about RAPI.
I finally got to that point on my list and started writing my version of RAPIStart. Did a little research and started writing a command line tool using C. My research told me that to start an app on the device from a workstation I would need to call CeCreateProcess(). To call CeCreateProcess() we first need to call CeRapiInit() or CeRapiInitEx() to establish a connection to the device.
My research also told me that CeRapiInit() has a tendency to hang if the connection isn’t established, but CeRapiInitEx() is a little friendlier to use because it times out. MSDN even has some sample code for establishing the connection, so I used it. The example function is TryRapiConnect() and since it is available on MSDN, I won’t reproduce it here.
So I created a Visual Studio command line application project and pasted TryRapiConnect() into the file and called it from _tmain(). Being impatient, I tried to compile knowing that I hadn’t done anything to ensure that it compiles like including rapi.h. or set it to link with rapi.h. So it failed. Now the adventure begins. Where do I get rapi.h and rapi.lib.   A little searching on my hard disk and I found rapi.h in the Platform Builder files, but no rapi.lib. I thought that it must be available someplace other than the Windows Mobile SDK, but I didn’t find it anyplace else, so I downloaded the SDK and installed it.   Then set the includes and lib paths correctly and it builds.
The next step is to create a little test to start a hardcoded application, in this case my registry editor from Windows CE: Simple Little Registry Editor. Here is the code:
int _tmain(int argc, _TCHAR* argv[])
                HRESULT hr;
                PROCESS_INFORMATION pi;   
                hr = TryRapiConnect(5000);
                if( SUCCEEDED(hr) )
                                if (!CeCreateProcess(_T("regedit.exe"), NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi))
                                                _tprintf( TEXT("Failed to start application (%d)\n"), CeGetLastError());
                                                CeCloseHandle( pi.hProcess);
                                                CeCloseHandle( pi.hThread);
                return 0;
Simple enough and it starts my registry editor when it runs. The next step is to handle the command line arguments for the application name and parameters.
But wait a minute, why am I wasting time developing a command line tool? Did I mention that the source code for a similar tool is available in the Windows Mobile SDK? Why start an app that is already on the device, RAPIStart does that already?
Why write a blog about this? Well it answers a few questions on how to get started, and it shows some C code for using RAPI.
Next step, write a C# GUI app – See Part 2 at Windows CE: Using RAPI to Run Applications (Part 2)
Copyright © 2009 – Bruce Eitman
All Rights Reserved