Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / February 2006

Tip: Looking for answers? Try searching our database.

Loadind freeing reloading an unmanaged dll in c# code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
antoine - 24 Feb 2006 13:14 GMT
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
Willy Denoyette [MVP] - 28 Feb 2006 20:50 GMT
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.

| Hello,
|
[quoted text clipped - 52 lines]
| thanks
| antoine

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.