Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#?

like image 775
Robin Rodricks Avatar asked Mar 24 '13 12:03

Robin Rodricks


2 Answers

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.

like image 197
David Heffernan Avatar answered Nov 14 '22 21:11

David Heffernan


If you are creating an object from a C++-DLL, it will actually persist until you delete it or rather dispose it. As you need to manually remove unmanaged objects, it will stay.

like image 37
bash.d Avatar answered Nov 14 '22 20:11

bash.d