Re: dllimport to unload dll
- From: Jeroen Mostert <jmostert@xxxxxxxxx>
- Date: Tue, 15 Jul 2008 00:10:18 +0200
Dan Holmes wrote:
I have this DllImport attribute on an external C++ dll. What i am noticing that the dll isn't unloaded after the static method is complete. I was thinking that when the method (it is a static method) completed then the dll would be unloaded.Well, first of all, the method being static or not has nothing to do with anything. Imported functions do not take a "this" parameter whether they're declared as static or not, and there are no special semantics associated with it. To lessen confusion it's a good idea to always declare an imported function as static, though.
Second, there is no way for the runtime to know that it's safe to unload the DLL. Many DLLs cannot be unloaded at all, at least not without crashing the executable that loaded them. They can start new threads, allocate resources, register callbacks, etc, and many have no proper cleanup code. It would be quite annoying to load a DLL, call a function that starts a background thread, and then have the runtime helpfully unload that DLL behind your back, which would either crash the thread or force the DLL to terminate it (barring some hax code injection).
[System.Runtime.InteropServices.DllImport(@"C:\dnlds\R3Rse_a.dll"There's always GetModuleHandle() and FreeLibrary() (twice) if you're sure the DLL can be safely unloaded. I don't know if explicit FreeLibrary() calls cooperate nicely with the interop loading mechanism, though. Personally, I wouldn't bother. About the only thing explicit unloading would be good for is to allow the DLL to be replaced while the main application is still running, which is a dubious versioning mechanism to say the least. .NET's explicitly unloadable AppDomains are a better match for that.
, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
Is there a way i can force this to unload or will i have to use loadlibrary?
--
J.
.
- Follow-Ups:
- Re: dllimport to unload dll
- From: Dan Holmes
- Re: dllimport to unload dll
- References:
- dllimport to unload dll
- From: Dan Holmes
- dllimport to unload dll
- Prev by Date: Re: Mind-boggling issue - text box not updating on Vista
- Next by Date: Re: Custom ITypeDescriptorContext
- Previous by thread: dllimport to unload dll
- Next by thread: Re: dllimport to unload dll
- Index(es):
Relevant Pages
|