Managed C++ Development
Syntax Battles
Here are a few syntax helpers which help if your moving from c# to C++/CLI
Attributes
C#
[Editor(typeof(ObjectReferenceEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(ObjectReferenceConverter))]
public struct ObjectReference
C++
[Editor(ObjectReferenceEditor::typeid, UITypeEditor::typeid),
TypeConverter(ObjectReferenceConverter::typeid)]
public ref class ObjectProxy
The Most Annoying Bug I've hit in C#
The other day I was working on an application that mixes C#, managed C++ and unmanaged C++. After linking in some of the static unmanaged libs into the project I hit this...
Everything compiled and linked fine with no warnings etc but when I ran the app it crashed/throws an exception here;
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
HERE-> Application.Run(new Form1());
}
The exception was as vague as one can possible be, trapping the exception and displaying the message etc didn't help.
First-chance exception at 0x7c812aeb in MyApp.exe: Microsoft C++ exception: HRException at memory location 0x0012dd04..
Turns out the static lib I'd linked to required a dll, that dll needs to be in the same folder as the .net application for it to find it or you get this exception.
The Empire Strikes Back (the most annoying problem episode II)
Managed Debugging Assistant 'BindingFailure' has detected a problem in 'D:\dev\f1_2011\programming\tools\animation\Area51\bin\Debug\Area51.vshost.exe'.
Additional Information: The assembly with display name 'ManagedWrapper' failed to load in the 'Load' binding context of the AppDomain with ID 1. The cause of the failure was: System.BadImageFormatException: Could not load file or assembly 'ManagedWrapper, Version=1.0.4281.14435, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
The problem is the managed c++ assembly contains unmanaged code - so you need to change the project settings in the c# project - change Any CPU to x86 in the Build panel.
Links
http://msdn.microsoft.com/en-us/library/b23b94s7(VS.80).aspx