all groups > dotnet interop > february 2006 >
You're in the

dotnet interop

group:

Loadind freeing reloading an unmanaged dll in c# code


Loadind freeing reloading an unmanaged dll in c# code antoine
2/24/2006 2:14:12 PM
dotnet interop:
Hello,

In a managed application (c#) , I load an unmanaged dll (c++), but
during the execution I need to re-init the dll many times, so I tried to
free
the unmanaged dll and reload it after, it seems to work only one time ,
has anyone an explication about potential side effects? let see the c# code
:

class MyDllWrapper {

[DllImport ("kernel32.dll")]
private extern static IntPtr LoadLibrary(string fileName);
[DllImport ("kernel32.dll")]
private extern static bool FreeLibrary(IntPtr lib);
[DllImport("kernel32.dll")]
private extern static IntPtr GetModuleHandle(string moduleName);
[DllImport("myunmanagedlib.dll")]
private extern static void TestUnManagedDll();

private IntPtr lib = IntPtr.Zero;

public Test()
{
LoadMyUnmanagedDll();
TestUnManagedDll(); // ---------> OK
UnloadMyUnmanagedDll();


LoadMyUnmanagedDll();
TestUnManagedDll(); // ---------> OK
UnloadMyUnmanagedDll();


LoadMyUnmanagedDll();
TestUnManagedDll(); // ---------> CRASH
UnloadMyUnmanagedDll();
}

protected void LoadMyUnmanagedDll()
{
lib = LoadLibrary("myunmanagedlib.dll");
}

protected void UnloadMyUnmanagedDll()
{
if(lib == IntPtr.Zero) return;
while (lib != IntPtr.Zero)
{
FreeLibrary(lib);
lib = GetModuleHandle("myunmanagedlib");
}
lib = IntPtr.Zero;
}

}


thanks
antoine

Re: Loadind freeing reloading an unmanaged dll in c# code Willy Denoyette [MVP]
2/28/2006 9:50:35 PM
Why are you unloading the DLL before the call to GetModuleHandle?
When you call FreeLibrary you unmap the dll, from the process, any call to
GetModuleHandle will fail when the module is not loaded (mapped). Another
point is that you should check your function return values to make sure you
effectively loaded the DLL.
Note also that you don't need to call LoadLibrary, PInvoke will do this
automatically when you call the function, all you need to do is to grab a
HMODULE and use this handle to unload the library (FreeLibrary).


Willy.

[quoted text, click to view]
| Hello,
|
| In a managed application (c#) , I load an unmanaged dll (c++), but
| during the execution I need to re-init the dll many times, so I tried to
| free
| the unmanaged dll and reload it after, it seems to work only one time ,
| has anyone an explication about potential side effects? let see the c#
code
| :
|
| class MyDllWrapper {
|
| [DllImport ("kernel32.dll")]
| private extern static IntPtr LoadLibrary(string fileName);
| [DllImport ("kernel32.dll")]
| private extern static bool FreeLibrary(IntPtr lib);
| [DllImport("kernel32.dll")]
| private extern static IntPtr GetModuleHandle(string moduleName);
| [DllImport("myunmanagedlib.dll")]
| private extern static void TestUnManagedDll();
|
| private IntPtr lib = IntPtr.Zero;
|
| public Test()
| {
| LoadMyUnmanagedDll();
| TestUnManagedDll(); // ---------> OK
| UnloadMyUnmanagedDll();
|
|
| LoadMyUnmanagedDll();
| TestUnManagedDll(); // ---------> OK
| UnloadMyUnmanagedDll();
|
|
| LoadMyUnmanagedDll();
| TestUnManagedDll(); // ---------> CRASH
| UnloadMyUnmanagedDll();
| }
|
| protected void LoadMyUnmanagedDll()
| {
| lib = LoadLibrary("myunmanagedlib.dll");
| }
|
| protected void UnloadMyUnmanagedDll()
| {
| if(lib == IntPtr.Zero) return;
| while (lib != IntPtr.Zero)
| {
| FreeLibrary(lib);
| lib = GetModuleHandle("myunmanagedlib");
| }
| lib = IntPtr.Zero;
| }
|
| }
|
|
| thanks
| antoine
|
|

AddThis Social Bookmark Button