Search code examples
c#interoppinvokeunmanagedmanaged-c++

Gracefully handle an unmanaged DLL crash on Managed side that is accessed via P/Invoke [DllImport]


I have a C# application, which has a reference to a CLI/C++ assembly, which interops with an unmanaged dll via a few extern "C" methods in the headers (The [DllImport] attribute).

Everything works great, if not a little convoluted. The project that all of this lives in wraps around this final unmanaged DLL for testing purposes, so its stability is iffy.

Is there any way with the current system above to not bring down the C# and cli/C++ assembly when the unmanaged DLL crashes? I don't believe there is, due to memory space mumbo jumbo, but SO has proven me wrong in many cases, thankfully.


Solution

  • I ended up having a watchdog application that is a totally separate process. This starts up the application that can be taken down by the bad dll, and since it has a handle to the process, it can try various things to detect a crash or shutdown.