Geeks With Blogs
Prabhu Kumar a tech twaddler..
I was working on this application which runs on the PC and gets information about the windows mobile device which is connected to the PC over active sync. Now there are a couple of ways in which you can do this:

1) Create a PC app which gets all the information using RAPI api's and displays it.

2) Create two binaries, one for the PC and one for the WinMob device. The PC app launches the WinMob app remotely using RAPI API's, the WinMob app runs and writes all the information it can gather into a file on the device. The PC app then remotely reads this file and displays it to the user. The let down is that you need to create/maintain two binaries but the advantage is that you are not limited to the RAPI api's, all device API's are at your disposal.

3) Create a PC app which uses the embedded exe approach. This is a bit loony but works (; And yes, this is what I used in my app.

Let me digress here for people who do not know about RapiConfig.exe. RapiConfig is tool that ships with the Windows Mobile SDK's and can be used to test out CSP's (Configuration Service Provider) by using provisioning XML's. RapiConfig works over Active Sync. So basically you run this tool from command line like:

C:\>RapiConfig /P config.xml

This command has the same effect as calling DMProcessConfigXML() API on the device with config.xml's contents.

And then I came across the DeviceInformation CSP. The DeviceInformation service provider, as the name gives it away, provides information about the device and it can also be used to set information but we won't go into that. So you give this service provider an XML like this:

<wap-provisioningdoc>
    <characteristic type="DeviceInformation">
       <parm-query name="OperatingSystem" />
       <parm-query name="OperatingSystemVersion" />
       <parm-query name="Product" />
       <parm-query name="ProductVersion" />
    </characteristic>
</wap-provisioningdoc>

and it spits out the response like this:

<wap-provisioningdoc>
    <characteristic type="DeviceInformation">
        <parm name="OperatingSystem" value="Microsoft Windows CE"/>
        <parm name="OperatingSystemVersion" value="Version 5.2 (Build 1235)"/>
        <parm name="Product" value="Windows Mobile® 6 Standard"/>
        <parm name="ProductVersion" value="CE OS 5.2.1235 (Build 17740.0.2.0)"/>
    </characteristic>
</wap-provisioningdoc>

So while developing this application I thought I could use some of RapiConfig. I shouldn't have to write everything from scratch. I'll just find out the SDK installation directory on the PC somehow, run this tool with my xml, parse the output xml and display it. But wait, what if the PC does not have the SDK installed. This simple application can't depend on that. And that is when I had this crazy idea of embedding RapiConfig.exe within my application (; I wrote up a small test app to find out if this approach would even work. It did. You can get the details here in my previous post.

So thats the way it works. The application first extracts the embedded RapiConfig exe and the xml file into the current directory where the app is running from. Then it runs RapiConfig passing it the extracted xml. After RapiConfig completes executing and terminates, it creates a RapiConfigOut.xml in the same directory. The application opens this output xml, parses it and displays the information to the user.

If no device is connected then RapiConfig keeps waiting for the device to connect. To prevent this I used a few RAPI api's in my application to find out if a device is connected using Active Sync, and only if it is, I launch RapiConfig exe. Otherwise, I display a message saying "Please make sure the device is connected.. blah bla"

A few points worth jotting down

(*) After extracting the RapiConfig exe, I was calling CreateProcess() on it to launch it:

wsprintf(lpCmdLine, L"/P %s", XML_FILENAME);
ret = CreateProcess(EXE_FILENAME, lpCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &sui, &pi);

But this didn't work for me. The output XML was the exact same as the input xml, without any data. Launching the same extracted exe from command line worked! How was this possible? I tried a couple of things with SECURITY_ATTRIBUTES, inheriting handles etc but none worked. The documentation for CreateProcess() mentioned that the first parameter to it maybe be NULL, in that case lpCmdLine parameter should include the exe name. And when I gave this a try, it worked! So now you call CreateProcess() like below:

wsprintf(lpCmdLine, L"%s /P %s", EXE_FILENAME, XML_FILENAME);
ret = CreateProcess(NULL, lpCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &sui, &pi);


(*) Some while back I had tried to fiddle with RAPI. I had tried to launch an exe on device remotely from the PC. But it didn't work. Turns out I was calling CeRapiInit() incorrectly. I was calling it directly, not on any object but actually you need to call it on the IRapiSession interface. I should really be reading the documentation carefully before jumping to the code (;


Here are a few snapshots of the application, this is with craddled emulator.













Oh and the DeviceInformation doesn't give you the resolution of the device. I used CeGetSystemMetrics() to get that part of information. And if there is any other details that you would want on this app, please leave a comment.

Good day! Posted on Thursday, October 22, 2009 11:09 PM Applications | Back to top


Comments on this post: Applications: Getting device information (embedded exe, rapi and more)

# re: Applications: Getting device information (embedded exe, rapi and more)
Requesting Gravatar...
hi Prabhu,
a very article!

Can you please share the code where you parse the output xml? Is this a std parser or a parser tuned to parse wap-provisioningdoc xml dtd?
Left by VG on Oct 25, 2009 5:21 AM

# re: Applications: Getting device information (embedded exe, rapi and more)
Requesting Gravatar...
VG,

Well, I parsed the output XML in the lamest way possible. I did not use any standard XML parsers. Although I have worked with Microsoft's SAX XML parser in the past, I thought it would be an overkill for this simple app. If you're looking for a full fledged XML parser I suggest you go through this excellent tutorial on SAX by Marius Bancila:

http://www.codeguru.com/cpp/data/mfc_database/xml/article.php/c11517__1/


Prabhu
Left by Prabhu Kumar on Oct 25, 2009 8:34 PM

# re: Applications: Getting device information (embedded exe, rapi and more)
Requesting Gravatar...
Why can you not just memory map the embedded resource and make the memory map executable? Then invoke it...don't ask me how, I've tried to do that before...That would seem easier to do than resorting to writing out to disk (which would require deleting when the program ends!)
Left by Tom Brennan on Feb 14, 2010 3:10 PM

# re: Applications: Getting device information (embedded exe, rapi and more)
Requesting Gravatar...
Tom, the embedded resource is already in main memory so why would you want to map it again? And even if you could, I don't think making the memory map executable is that straight forward. I searched around a bit but didn't find any useful information.

And you say that you tried to do that before, I would like to know if you were successful. I read a few things about 'Data Execution Prevention' which prevents just any code in your process space from running by marking the memory as non-executable, and I think the embedded resource file is a good candidate for non-executable code. So until then, 'resorting to writing out to disk and deleting when the program ends' seems easier to me. Will continue to investigate though.
Left by Prabhu on Feb 16, 2010 9:55 PM

Your comment:
 (will show your gravatar)


Copyright © TechTwaddle | Powered by: GeeksWithBlogs.net