This is a mirror of official site: http://jasper-net.blogspot.com/

How to release an unmanaged library loaded into managed .NET code

| Monday, February 20, 2012
Motivation

I had found this article on how to release a DLL library already loaded into a process using P-Invoke. It uses the LoadLibrary() and FreeLibrary() WinAPI calls to achieve this.

And what is wrong with it?

It forces to unload all instances of the DLL library currently loaded within the process. Which means, that in the case you have more than one instance of the class using these external functions, all these will stop working!

And that is not all - you cannot use this DLL in the same application domain again after unloading.


Solution

The solution is a pretty simple one, but I have to say that it wasn't very obvious to me at the beginning. You can use P-Invoke to import the following standard WinAPI functions for dynamical function loading:

    LoadLibrary()
    FreeLibrary()
    GetProcAddress()

We will use the following wrapping class:

internal static class UnsafeMethods
{
    [DllImport("kernel32.dll", SetLastError = true)]
    internal extern static IntPtr LoadLibrary(string libraryName);
    [DllImport("kernel32.dll", SetLastError = true)]
    internal extern static bool FreeLibrary(IntPtr hModule);
    [DllImport("kernel32.dll", SetLastError = true)]
    internal extern static IntPtr GetProcAddress(IntPtr hModule, string procName);
}

We also need signatures of the imported functions - we will convert them into delegates (the following ones come from the sample project):


// int multiply(int value1, int value2);
private delegate int MultiplyDelegate(int value1, int value2);
// int str2int(const char *input);
private delegate int Str2IntDelegate([MarshalAs(UnmanagedType.LPStr)]string source);


Read more: Codeproject
QR: Inline image 1

Posted via email from Jasper-net

0 comments: