Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled Exceptions from Managed C# User Control used in MFC Dialog

Our core application is built in MFC C++, but we are trying to write new code in .NET, and have created a User Control in .NET, which will be used on an existing MFC Dialog.

However, when a unexpected/unhandled exception is thrown from the User Control, it causes the MFC app to crash (illegal op style), with no ability to recover.

I've added

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

To the Constructor of the .NET user control, but it doesn't seem to catch anything.

Is there any way to add an event in MFC to handle these?

Quick google didn't return anything useful.

Thanks.


Edit: Still haven't been able to resolve this the way I'd like, looks like the best way to do it, is try and catch around all the .NET code, so no exceptions bubble up.

like image 424
PostMan Avatar asked Nov 21 '25 01:11

PostMan


1 Answers

I asked this same question a while ago: Final managed exception handler in a mixed native/managed executable?

What I have found is that the managed unhandled exception events ONLY fire when running in a managed thread. The managed WndProc is where the magic happens.

You have a few options: you could place a low-level override in CWinApp and catch Exception^. This could have unintended side-effects. Or, you could go with Structured Exception Handling (SEH) which will give you a hack at /all/ unhandled exceptions. This is not an easy road.

like image 159
Aidan Ryan Avatar answered Nov 23 '25 14:11

Aidan Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!