Things mom never told you about C++ CLI

For the last few months I’ve been working on a project that utilizes C++/CLI to bridge some legacy code with new code written in C#. A good chunk of that time has been spent running repeatedly into walls, pitfalls, quirks, and “known issues.” I figured I’d document some of the issues I’ve encountered in case someone out there to hopefully save people out there a few hours of frustration.

0) Preparation

  • The following are the books I’ve located dedicated to C++ CLI. The biggest problem with these books is they cover very simple scenarios and assume things “just work” when in reality they DON’T quite often and you end up on Google and Stack overflow trying to figure out some cryptic error message.

1) Avoid Visual Studio 2012 Update 2

If you’re doing C++/CLI work Update 2 is the devil! It breaks the mixed mode debugger. Relevant links:

As a side note, XP Compatibility compilation is now broken:

And last but not least uninstalling Update 2 *may* leave your Visual Studio install crippled:

 

1) Compile time and link time errors

Some of these could depend heavily on exactly what you are doing so though you may get the same error you may have a completely different problem/solution. Contact me if you have an issue+solution you’ve discovered and want added.

A) Error: “String cannot be of zero length. Parameter name: frameworkName”

Cause: incorrectly created file located at C:\Users\YOURNAME\AppData\Local\Temp\.NETFramework,Version=v4.0.AssemblyAttributes.cpp

Fix1: The file should be written every build, so just delete teh file

Fix2: Open the file and confirm first parameter is empty string ( L""). If so then add appropriate framework to first parameter, as shown below in red:

#using <mscorlib.dll>
[assembly: System::Runtime::Versioning::TargetFrameworkAttribute(L".NETFramework,Version=v4.0", FrameworkDisplayName=L".NET Framework 4")];

Answer found here: http://stackoverflow.com/questions/13315940/apps-exe-file-missing-net-targetframework-but-only-on-clean-builds

 

B) Error: “String cannot be of zero length. Parameter name: frameworkName”

Cause: incorrectly created file located at C:\Users\YOURNAME\AppData\Local\Temp\.NETFramework,Version=v4.0.AssemblyAttributes.cpp