rsantos
rsantos

Reputation: 230

How to detect a Process Exit from a DLL in C#

I have a WPF Application that is calling functions inside a native DLL (written in c++). This functions in the .DLL sometimes do a Process Exit that is killing the WPF application.

Example:

WPF App:
.....
[DllImport("native.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern void NativeMethod();
.....
void CallFunction()
{
  //Call the native function
  NativeMethod();
}
....

Inside the NativeMethod there is an exit and I cannot change the source code of the .dll. Anyone know a way to capture this exit in the DLL from .NET code ?

Thanks in advance,

Upvotes: 5

Views: 2476

Answers (2)

rsantos
rsantos

Reputation: 230

AppDomain.ProcessExitOk, the problem using AppDomain's was they only work with .NET assemblies :( so I had to create an assembly that loads my native DLL via P/Invoke. Once I had that, I could Load the assembly in a different AppDomin and call the functions of the native .dll throw managed .dll. I capture the AppDomain.ProcessExit event from the AppDomain and I could capture the exit ;).

Thanks aiampogi for the idea ;)

Upvotes: 4

aiapatag
aiapatag

Reputation: 3430

On top of my head, the only thing I can think of is load that DLL to a process/app domain (more on an appdomain) then invoke that method and register for the ProcessExit event to handle it properly.

I might try to do this later but I'm just not sure if you can invoke static methods on an appdomain or invoke a non-serialized class method on an appdomain.

Upvotes: 2

Related Questions