This blog entry explores if it is possible to run VB.NET code on a .NET Micro Framework device. The current Visual Studio integration for the .NET Micro Framework only supports C# development, but since the framework is based upon the CLR runtime it theoretically supports development in any language that can be compiled down to Microsoft Intermediate Language (MSIL) code. So can we compile a VB.NET application for a .NET Micro Framework device and if so what kind of limitations or issues are we likely to come across? Read on to find out the results of some early experiments…
A sample application
The first thing we need is some VB.NET code to attempt to compile. For this I have written the following WPF application.
Imports System Imports Microsoft.Spot Imports Microsoft.SPOT.Presentation Imports Microsoft.SPOT.Presentation.Media Imports Microsoft.SPOT.Presentation.Shapes NameSpace HelloWorldVB Public Class Program Public Shared Sub Main() Dim app as Application = New Application() ' Create a window object and set the size to ' the size of the display Dim mainWindow as Window= New Window() mainWindow.Height = SystemMetrics.ScreenHeight mainWindow.Width = SystemMetrics.ScreenWidth ' Add a green rectangle as the sole child ' control of the window Dim rect As Rectangle = new Rectangle() rect.Fill = new SolidColorBrush(CType(&HFF00, Color)) mainWindow.Child = rect ' Make the window visible mainWindow.Visibility = Visibility.Visible app.Run(mainWindow) End Sub End Class End Namespace
This application simply displays a full screen window that is coloured Green. This is a reasonably simple application that doesn’t make use of too many VB.NET language or .NET Micro Framework features so is an ideal starting point.
Compiling the application
The first step is to use the standard VB.NET command line compiler (vbc.exe) to compile the source code into a standard executable. We can do this by using the following commands at a command prompt:
SET NET_BIN=C:\Windows\Microsoft.NET\Framework\v2.0.50727 SET MF_BIN=C:\Program Files\Microsoft .NET Micro Framework\v2.0.3036\Tools SET MF_LIB=C:\Program Files\Microsoft .NET Micro Framework\v2.0.3036\Assemblies "%NET_BIN%\vbc.exe" /netcf /define:_MYTYPE=\"EMPTY\" /reference:"%MF_LIB%\Microsoft.SPOT.Graphics.dll" /reference:"%MF_LIB%\Microsoft.SPOT.TinyCore.dll" /out:HelloWorldVB.exe /target:exe Program.vb
This is all fairly standard stuff for command line VB.NET compilation, however there are two things to note. First we have used the /netcf argument which causes the compiler to target the .NET Compact Framework. This removes language functionality (such as late binding) that depend upon aspects of the full .NET framework.
The second thing to note is the define which sets _MYTYPE to EMPTY. As discussed by the “Customizing Which Objects are Available in My” article on MSDN setting this define to EMPTY will cause the VB.NET compiler to not generate a My namespace. The My namespace is typically used by VB.NET developers to get quick access to convenience functions for things such as File IO and networking functionality, but the code emitted by the VB.NET compiler is not suitable for the .NET Micro Framework base class libraries.
Transforming the executable
Now that we have compiled the VB.NET code into MSIL we need to run the executable through an application called the Meta Data Processor. This pre-processes the MSIL based executable into a more efficient and smaller format that is suitable for use by the interpreter that forms the core part of the .NET Micro Framework runtime.
The following command line will convert the HelloWorldVB.exe executable into a HelloWorldVB.pe file that is suitable for execution by the .NET Micro Framework.
"%MF_BIN%\MetaDataProcessor.exe" -loadHints mscorlib "%MF_LIB%\mscorlib.dll" -parse HelloWorldVB.exe -minimize -compile HelloWorldVB.pe
You should notice that HelloWorldVB.pe is significantly smaller than HelloWorldVB.exe and even while viewing it using Notepad it clearly shows that there is less unnecessary “baggage”. For instance there is no string stating “This program cannot be run in DOS mode”.
Running the application
Now that we finally have what appears to be a compiled VB.NET application in a form that is usable by the .NET Micro Framework we need to figure out a way to load it into our .NET Micro Framework emulator or device.
The following command line launches HelloWorldVB.pe within the sample emulator that comes with the .NET Micro Framework SDK.
"%MF_BIN%/Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe" /load:HelloWorldVB.pe /load:"%MF_LIB%\mscorlib.pe" /load:"%MF_LIB%\Microsoft.SPOT.Native.pe" /load:"%MF_LIB%\Microsoft.SPOT.TinyCore.pe" /load:"%MF_LIB%\Microsoft.SPOT.Graphics.pe"
At this point in time I have not investigated how to deploy *.pe files to actual devices.
Conclusion
This experiment demonstrates that at a technical level it should be possible for the .NET Micro Framework to eventually support the execution of applications written in VB.NET. It does however demonstrate that at present there are a couple of hurdles to overcome, mainly in the code generated by the VB.NET compiler having dependencies on framework functionality not present by the .NET Micro Framework. Not being a VB.NET expert I wonder how far you can go before finding other problems? My next step is to figure out how to deploy *.pe files to an actual device so that I can verify that HelloWorldVB.pe actually does run on a physical device.
I am also interested in investigating the MetaDataProcessor.exe application in further depth. Is anyone aware of additional documentation for this tool? By running it from the command line it is apparent that it has a wide range of command line arguments, but I have presently been unable to find information about it usage.

We also need to add a couple of additional references to our Project in order for the compiler to find the WPF related classes:

The sample application available for download demonstrates a number of basic drawing operations as discussed above. The application cycles through a number of demonstrations. The sample application also demonstrates the use of System.Reflection functionality within the .NET Micro Framework to find the examples. If you would like to experiment with the drawing APIs, this sample application would be an ideal test harness, just add another “Example_XYZ” method that contains your drawing code and your example will be automatically picked up.
Another sample application is available for download (without explanation as to how it is implemented). This example helps demonstrates the rendering capabilities of the .NET Micro Framework by creating and animating up to 50 random rectangles of different size, colour and alpha transparency over top of the .NET Micro Framework snowflake logo. It also demonstrates the fact that the .NET Micro Framework emulator is really a simulator. You will notice that running this example under the emulator produces very impressive rendering speeds which are not matched when running on actual hardware.