I have a tricky combination of C#, C++/CLI and native C++ code. In short, what I have:
- A third-party managed application, namely, the Visual Studio 2010 process
- A C++/CLI dll which is loaded by this process, namely, an Evaluator Add-In
- A C# assembly with a
Windows.Froms.Formclass in it, which is shown by the dll
I show my C# form from the C++/CLI dll with the ShowDialog method:
System::Reflection::Assembly^ GUI = System::Reflection::Assembly::LoadFrom("MyCppDll.dll");
Type^ FormMain = GUI->GetType("MyCsNamespace.FormMain");
MethodInfo^ ShowDialog = FormMain->GetMethod("ShowDialog", gcnew array<Type^>{});
System::Object^ form = System::Activator::CreateInstance(FormMain, args);
ShowDialog->Invoke(form, nullptr);
All works fine, except I don’t know how to handle exceptions which may be thrown during form execution. I wish the ShowDialog->Invoke call just throws these exceptions, but it ain’t.
I’ve tried to set AppDomain::UnhandledException handler in C++, but it is not triggered. The weird thing, I tried to set it in C#, but it is not triggered either.
So how do I handle exceptions in C++ dll which is thrown in C# assembly in this situation? I have full control over C++ and C# code, but the host application is closed.
ShowDialog->Invokewill throw a TargetInvocationException; the InnerException of this will be the exception that the form throws.