« Home | Just started »

The Microsoft Visual C++ /clr Switch

Today I had a go at compiling a large body of existing legacy C/C++ code with the /clr switch. The bulk of it compiled without changing the existing source code. Most of the compile problems looked fairly trivial to resolve as they were related to the use of older C/C++ syntax in the code. The purpose of the exercise was to see if the existing (non COM) C/C++ code could be used directly within a .NET application without using p/invoke. The ideal would have been able to just refer to the existing C++ classes in newly developed C# code. Unfortunately, the Reflector utility shows that the compilation output "mangles" the C++ classes somewhat. A .NET struct is created to represent the class and then a series of static methods created for the methods of the class (which weren't necessarily static in the header file). The Microsoft developers must have done this to be able to practically implement all the C++ features (such as multiple inheritance) within the .NET environment. If the __gc keyword is used on a C++ class then the compilation output is a class derived from System.Object and all the methods defined in the header become part of the class i.e. not just a struct and a bunch of static methods. Using the __gc keyword would allow better interoperability with VB.NET/C# code but introduces more C++ compilation restrictions. Oh well, can't have everything! Just the ability to compile existing C++ code to the CLR is pretty amazing.

Links to this post

Create a Link