Search code examples
c#c++.netdllpinvoke

Does P/Invoke execute the DLL and then shut it down?


If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory? Or will .NET take charge of the memory used by the C++ DLL in an unmanaged "heap" and give pointers to those objects to the C++ DLL everytime I call a static function?

When I need a certain C++ project to have its memory persistant, should I be creating an ActiveX/COM Server to have its memory persist, and yet be able to call it from C#?


Solution

  • If I use C# to P/Invoke a certain DLL, will the actual C++ DLL be run for the duration of the call and then be shut down, destroying all used memory?

    No. Once the DLL is loaded it will stay loaded. The DLL's lifetime is not tied to a function call. This means that variables in the DLL that have static storage persist beyond the initial p/invoke call.