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 / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Closing an unmanaged DLL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dvestal@gmail.com - 22 Feb 2008 18:56 GMT
I have a C# app that must use a C# class library, which in turn
references an unmanaged C++ DLL.  My code interacts briefly with the
C# class library, after which my code must fully release it, retaining
no references to either the class library or the DLL (which the class
library uses via DllImport).

Unfortunately, neither the class library nor the C++ DLL are under my
control, and I can't change the constraints above.

I've managed to release the C# dll by interacting with it in a
temporary AppDomain, which I then unload.  However, my process still
retains a reference to the C++ DLL.  How can I either avoid that, or
close the reference?
Nicholas Paldino [.NET/C# MVP] - 22 Feb 2008 19:05 GMT
The only way I can think of to do this would be to not use the P/Invoke
layer directly, and instead use calls to LoadLibrary/FreeLibrary along with
GetProcAddress to get a pointer to the functions you are calling, then call
the static GetDelegateForFunctionPointer method on the Marshal class to get
a delegate which you can call which will perform the call to the unmanaged
code.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I have a C# app that must use a C# class library, which in turn
> references an unmanaged C++ DLL.  My code interacts briefly with the
[quoted text clipped - 9 lines]
> retains a reference to the C++ DLL.  How can I either avoid that, or
> close the reference?
dvestal@gmail.com - 22 Feb 2008 19:22 GMT
On Feb 22, 2:05 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
>     The only way I can think of to do this would be to not use the P/Invoke
> layer directly, and instead use calls to LoadLibrary/FreeLibrary along with
[quoted text clipped - 24 lines]
> > retains a reference to the C++ DLL.  How can I either avoid that, or
> > close the reference?

Nicholas,

Unfortunately, neither the class library nor the managed code are
under my control.  I can't change how they're written.  As a last
resort, I can interact with them in another process entirely, then
kill that process, but I really don't want to do that.  The old
analogy of using a cannon to kill a gnat comes to mind.
Willy Denoyette [MVP] - 22 Feb 2008 20:15 GMT
> On Feb 22, 2:05 pm, "Nicholas Paldino [.NET/C# MVP]"
> <m...@spam.guard.caspershouse.com> wrote:
[quoted text clipped - 39 lines]
> kill that process, but I really don't want to do that.  The old
> analogy of using a cannon to kill a gnat comes to mind.

You can get the module handle by calling GetModuleHandle and use this handle
in a call to FreeLibrary.
Here are the DllImport declarations and a usage sample.

       [DllImport("kernel32")] static extern IntPtr GetModuleHandle(string
module);
       [DllImport("kernel32")]
       [return: MarshalAs(UnmanagedType.Bool)]
       static extern bool FreeLibrary(IntPtr handle);

...
   IntPtr HModule = GetModuleHandle("nativeModule.dll");
   bool success = FreeLibrary(HModule);
   if(success)
       ...
   else
       ...

Willy.

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.