Once again it was worthwhile simply exploring through a development environment. In preparation for a class I will attend next week I was just looking through Platform Builder 5.0, creating a simple platform and adding a number of native applications to it. Looking at the different project settings I was intrigued by the managed code tab:
Would it perhaps be possible to create managed code projects inside Platform Builder and if true, how could I have missed that option? The Platform Builder online help even tells you how to setup your managed project. Simply search for Using managed code in a project in the Platform Builder documentation. Of course this cried for a little test, so I created a new managed project and added an existing C# source file to it. The application is a very simple application that creates a number of worker threads, simulates some processing and writes some information to the UI. However, for a first trial this application was too complex, I got a bunch of error messages, most likely to do with the fact that I need to add a couple of references, although I don’t have a clue yet how to add those. So before continuing getting this application up and running I decided to create an even simpler application, one of the most basic Hello, World applications. Here is the source code for this little application:

It turned out that I had to exactly follow all steps that are listed in the Platform Builder documentation to get my project build and added to my platform. Even though the application compiled correctly to IL (see figure 2), I still got an error while generating a Windows CE image.

After some thinking and experimenting it turned out that the bib file, belonging to the managed application needed modification. The original contents of the bib file were:

The MODULES section for the MyManagedApp.exe indicates the Windows CE loader that it should load this executable the standard way as an application. Since a managed application runs inside the CLR, it should not use the common loading mechanism. Changing the MODULES section in the bib file to a FILES section takes care of this.
After building the Windows CE image, with the managed project included, it is indeed possible to run the managed application. Verifying the executable file using ildasm showed that the managed project compiled C# code to MSIL, just as Whidbey does (or of course Visual Studio.NET 2003).

As you can see, the generated IL looks fine. The only difference between the code generated by Platform Builder (or probably just by the C# command line compiler that comes as part of the .NET Framework although I am only guessing here) and the code generated by Whidbey is a larger stack size reservation and an extra nop instruction.
After building the managed project and creating a runtime image of the Windows CE Operating System there was only one thing to find out. Is it possible to start the just created managed application? To find out I booted the Windows CE image into the Windows CE Emulator, used explorer to navigate to the managed application and started it. Here is the result:

As you can see, the application is running inside the Windows CE Emulator. Since it uses Console.Writeline to display information, a console Window was automatically opened as well.

Moving on to a managed WinForms Application
Creating a managed WinForms application using Platform Builder turned out to be a very simple extension to the already created console application. There are only a few changes involved. First off, the target type under the General tab of the Project Settings dialog needs to be changed from Managed Application to Managed Window Application. The next thing to do is add WinForms specific references to the managed code tab of project settings. For the example that I tried I needed to add the following references:
$(_MANAGED_FRAMEWORK_DIR)\mscorlib.dll;
$(_MANAGED_FRAMEWORK_DIR)\system.dll;
$(_MANAGED_FRAMEWORK_DIR)\system.drawing.dll;
$(_MANAGED_FRAMEWORK_DIR)\system.windows.forms.dll;
$(_MANAGED_FRAMEWORK_DIR)\system.data.dll
After that it was simply a matter of building the project and adding it to the Windows CE image. The result was a multi-threaded WinForms application that simply creates a number of threads and destroys them again, with the main thread waiting until all worker threads have been destroyed. Running in the emulator, the application looks like this:

To verify that the application was working properly I also took a look at the remote kernel tracker. As you can see the managed application has run, created 10 worker threads and removed the 10 worker threads properly as well.

Oh, in case you are interested, here is the code for the multi-threaded WinForms application that I used to test building a managed Windows application. I have to admit that I first created this application in Visual Studio.NET 2003, because Platform Builder lacks a forms designer. In the code snippet you can only see the main thread creating the worker threads and the tread procedure that is shared by the worker threads. The actual user interface code is omitted.

Just for fun reasons all of this is pretty cool. We now can create managed applications using Platform Builder 5.0. There is one big question left. Why? Using Platform Builder definitely is not ideal when you are creating an application from scratch. Visual Studio is a much nicer development environment for application development, so I am curious why Microsoft decided to add support for managed application development inside Platform Builder.
You should be aware that support for building managed applications inside Platform Builder is limited to C# projects. That does not mean that you can’t run managed applications that are written in VB.NET, but you can’t build those from scratch using Platform Builder only. |