Applications: Painting problems with windowed directdraw app

Well, the painting problem that I mentioned in my last post is solved. Thanks to Joel for helping me out (:


When I look in retrospect, the answer was in the question all the time. Painting problem, WM_PAINT message. It was stupid of me to have missed that (; But looking at the bright side I have a much better understanding of how WM_PAINT works now. The only thing that I needed to do was to display the frame every time my window got a WM_PAINT message. So that our client area is re-drawn and is displayed correctly. Just call DisplayFrame() inside WM_PAINT. That is all.


Here’s a video of how things are working now, pretty smooth (:


How to embed an exe inside another exe as a resource and then launch it

While working on a utility project today, I stumbled upon wanting to embed an executable inside another executable. Sounds fun doesn’t it? And what is even more fun is to be able to launch the embedded exe!


Basically, here’s how it works. You embed Foo.exe inside Bar.exe. And by embed I mean, add Foo.exe as a resource in Bar.exe. And then from Bar.exe’s code, you can launch Foo.exe using CreateProcess().


So before answering the "Why?" lets answer the "How?"


Rename Foo.exe to Foo.txt. We do this just to be safe and to prevent the resource compiler (manager) from throwing unwanted errors. Now add Foo.txt as a normal resource in Bar.exe. Create an entry in Bar.exe’s resource script as below:

IDR_FOO     RCDATA       "Foo.txt"

And of course, you need to #define IDR_FOO in the resource header file. Just make sure its a unique value.


The steps are:


1) From within Bar.exe’s code, get a pointer to the first byte of Foo.txt
2) You should know the size of Foo.txt in bytes.
3) Using the pointer copy that many bytes into a separate file. ("\\Voila.exe")
4) Call CreateProcess() on "\\Voila.exe"
5) And voila!


Let’s dive into the code: (from the entry point of Bar.exe)

    HRSRC hrsrc = NULL;

    HGLOBAL hGlbl = NULL;

    BYTE *pExeResource = NULL;


    DWORD size = 7168;//hardcoding the size of the exe resource (in bytes)

    hrsrc = FindResource(hInstance, (LPCWSTR)IDR_FOO, RT_RCDATA);

    if (hrsrc == NULL)

        return FALSE;

    hGlbl = LoadResource(hInstance, hrsrc);

    if (hGlbl == NULL)

        return FALSE;

    pExeResource = (BYTE*)LockResource(hGlbl);

    if (pExeResource == NULL)

        return FALSE;



    if (hFile != INVALID_HANDLE_VALUE)


        DWORD bytesWritten = 0;

        WriteFile(hFile, pExeResource, size, &bytesWritten, NULL);



    int ret = CreateProcess(L"\\Voila.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi);

First, we find the resource using its resource identifier and then load it. Next we use LockResource() to get the pointer to the first byte of the resource data, which in this case would be the executable code. One downside, if you may say so, is that you need to know the exact size of the executable beforehand. Of course its easy to find out and I think its not a problem to hardcode because the size of the embedded executable won’t change. But if you still insist, then you can read it from the registry or a file or something.


Once you get the pointer, just copy all the bytes into another file, using WriteFile() API.


And finally do a CreateProcess() on the file you just created.


If you happen to know any alternate ways of doing this, please leave a message.


And coming to the important question of  "Why would any sane person want to embed an exe within an exe?" Well, you will have to wait till the next post to find out (;


I have changed the code above to copy all the 7168 bytes into "Voila.exe" in one go, instead of copying byte after byte. And just to be clear this is not a production code, it is just to demonstrate what can be done. Of course, creating the file "Voila.exe" in the root folder is not ideal and it may fail on many devices, and you also need to clean up by deleting the exe file, which can be done using the DeleteFile() API. Ideally, "voila.exe" should be created not in the root folder but instead using SHGetSpecialFolderPath() API passing for e.g. CSIDL_APPDATA. I left out all these details because I thought they were trivial for this post.

Applications: Appending text to a multiline Edit control

Today I was developing a small utility application where I needed to append some text to a multiline edit control. When I ran through the list of Edit control messages, I could not find any that could be used to append text. WM_SETTEXT was overwriting the previous contents. And doing a WM_GETTEXT first, appending to it and then WM_SETTEXT seemed like an overkill.


A little bit of searching led me to EM_REPLACESEL message. This message is used to replace the currently selected text in the edit control with the specified text but there’s a bit more to it. If there is no selected text in the edit control then the specified text is inserted at the current caret position. Since what I was using was a read-only edit control this works for me but I am not sure if it will work in all cases. I wonder if we need a SETCARETPOS message. So anyways, here’s how to append text in an edit control:

HWND hWndEdit = GetDlgItem(g_hDlg, IDC_EDIT_BOX);


Applications: Getting device information (embedded exe, rapi and more)

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:


    <characteristic type="DeviceInformation">

       <parm-query name="OperatingSystem" />

       <parm-query name="OperatingSystemVersion" />

       <parm-query name="Product" />

       <parm-query name="ProductVersion" />



and it spits out the response like this:


    <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)"/>



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!