# Sunday, 25 February 2007

One of the things that C++ can do that C# can't is MSIL linking from the IDE. Normally when you have a solution with two projects, a build creates two assemblies (one .exe and one .dll) for you to deploy. Sometimes you want to deploy only a single file, but you want two projects. Sometimes you want the advantages of static linking while you're creating MSIL and deploying onto the CLR. So how do you do it?

I created a tiny little solution with two projects, both in C++. One is a console application and the other is a library. I set the library to compile as /clr:safe and the console app as just plain /clr. In the console app I added a reference to the library.

At this point if I build and run I will have HelloWorld.exe and Library.dll.

Now I went to the Library project settings and made a few changes. First, on the Advanced tab of Linker options, I turned off assembly generation:

This gets the linker to make an assembly fragment called a .netmodule rather than an assembly. Next I changed my output file name:

Then I deleted the reference in the console application, and re-added it but this time to Library.netmodule instead of Library.dll. One more thing, the linker inputs in the console application need to include the .netmodule:

 

At this point I can build the solution, and delete Library.netmodule, Library.dll and the like and HelloWorld.exe will run just fine. I don't need to deploy Library because it's actually inside HelloWorld.exe - that's MSIL linking.

I have a mentoring client who is using this right now. They have an MFC application with a mix of native and managed code and it can't be compiled /clr:pure. They want to add some WinForms controls to that application, and the designer can only work with verifiable assemblies. So now one project is the user controls, and it's /clr:safe, and the the main exe is a mixed assembly. Yet they only deploy a single file in the end.

Kate

Sunday, 25 February 2007 19:18:55 (Eastern Standard Time, UTC-05:00)  #