Re: dllimport to unload dll

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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"
, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]

Is there a way i can force this to unload or will i have to use loadlibrary?

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.

--
J.
.



Relevant Pages

  • Re: dllimport to unload dll
    ... I was thinking that when the method (it is a static method) completed then the dll would be unloaded. ... 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. ... CLR hosting in SQL Server is subject to pretty serious restrictions precisely because they didn't want just any old code to execute as part of SQL Server itself. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: dllimport to unload dll
    ... I was thinking that when the method (it is a static method) completed then the dll would be unloaded. ... 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. ... 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. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Dynamically Created AX Control not released.
    ... every dll loaded in a process must be explicitly ... For COM dll's (like OCX) the dll is loaded in a compatible apartment (STA ... yourself, by calling "CoLoadLibrary", and unload it when done by calling ... > protected override void Dispose(bool disposing) ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Any tool that tells COM dependencies of a DLL?
    ... Actually that's what Igor was trying to tell you - the DLL never ... client does a single cocreate, ... one can unload them explicitly by calling ...
    (microsoft.public.win32.programmer.ole)
  • Re: Any tool that tells COM dependencies of a DLL?
    ... i see the shell load my ... i don't see the DLL get loaded and held onto... ... the client does a single cocreate, ... one can unload them explicitly by calling ...
    (microsoft.public.win32.programmer.ole)