Department of InformatiX
Microsoft .NET Micro Framework Tools & Resources

Visual Basic in .NET Micro Framework 4.4

This is an investigation story, solution is at the bottom.

If you have tried to debug a .NET Micro Framework 4.4 Visual Basic application in the emulator, you have probably hit an issue with deployment:

Link failure: some assembly references cannot be resolved!! Assembly: Microsoft.SPOT.Native (4.4.0.0) needs assembly 'mscorlib' (4.4.0.0) Assembly: MFConsoleApplication1 (1.0.0.0) needs assembly 'mscorlib' (4.4.0.0) Assembly: MFConsoleApplication1 (1.0.0.0) needs assembly 'Microsoft.VisualBasic' (1.0.0.0) Assembly: MFConsoleApplication1 (1.0.0.0) needs assembly 'Microsoft.SPOT.Native' (4.4.0.0) Error: a3000000

Clearly, there is a rush for mscorlib and Microsoft.VisualBasic assemblies, but if you switch the Output window to the Micro Framework Device Deployment source:

Micro Framework Device Deployment output window

you can see that neither of them is being deployed. Indeed, if you check the project references:

Project references

they are not there. If you try to add the references manually, nothing happens. That's because the compiler refences them implicitly, it simply assumes they are always available (and referenced). Unfortunately, the .NET Micro Framework does not fulfill this assumption. Therefore, the solution would be to make the reference to these assemblies explicit, so that the project system can pick them up and deploy to the emulator. Well, we cannot add the references in Visual Studio, because it thinks it knows better, but what if we add them in the project file directly?
... <ItemGroup> <Reference Include="Microsoft.SPOT.Native"/> <Reference Include="Microsoft.VisualBasic"/> <Reference Include="mscorlib"/> </ItemGroup> ...

It doesn't like it:

vbc : Fatal error BC2000: compiler initialization failed unexpectedly: Project already has a reference to assembly mscorlib. A second reference to 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.4\Assemblies\le\mscorlib.dll' cannot be added.

vbc : Fatal error BC2000: compiler initialization failed unexpectedly: Project already has a reference to assembly Microsoft.VisualBasic. A second reference to 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETMicroFramework\v4.4\Microsoft.VisualBasic.dll' cannot be added.

Quoting the documentation, one possible cause of this error is a reference to another copy of the assembly through a different file path than that of the original reference. That means not only that the compiler is already referencing mscorlib and Microsoft.VisualBasic assemblies on its own, but also that it is referencing different ones. If we only added another reference to the same assembly, it would have been fine.

Good news: the Visual Basic compiler allows us to specify which Microsoft.VisualBasic assembly is the right one. After quick peek into the Visual Basic targets file (at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.VisualBasic.targets) and figuring out the legacy mess with the help of Roslyn sources, it looks like the <VBRuntime/> property is what we need. This is of course the right thing to do in any case, but our primary motivation here is the ability to reference the assembly twice (explicitly in the project).

Bad news: there is no equivalent option for mscorlib. Good news (did you think we are going to end up with bad news?): there is another way how to deploy an assembly to the emulator — deploying it manually! Looking at the deployment process above, we just need to add an extra parameter to the emulator, to /load the mscorlib. Fortunately, the emulator takes additional command line options from a registry value called... AdditionalCommandLineOptions. Note that this option is used regardless of the programming language, so we are lucky we need it for mscorlib only, and that the emulator is happy with duplicates since the C# compiler already specifies the assembly explicitly. Ready for the summary?

3 easy steps to make Visual Basic working with the legacy compiler

  1. Add "/load:C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.4\Assemblies\le\mscorlib.pe" command line option into the registry (including the quotation marks).
    For the standard emulator, you need to create an AdditionalCommandLineOptions string value under the HKLM\SOFTWARE\WOW6432Node\Microsoft\.NETMicroFramework\v4.4\Emulators\Microsoft key.
     
  2. Add <VBRuntime>C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.4\Assemblies\le\Microsoft.VisualBasic.dll</VBRuntime> to the project file.
    You probably want to modify the .NET Micro Framework targets file instead ("C:\Program Files (x86)\MSBuild\Microsoft\.NET Micro Framework\v4.4\VisualBasic.targets" ) so that all Visual Basic projects work as they are.
     
  3. To the same file, add the explicit runtime reference: <ItemGroup> <Reference Include="Microsoft.VisualBasic"/> </ItemGroup>
Comments
Sign in using Live ID to be able to post comments.