I\'m using the LoadLibrary function to load a DLL in Windows. My question is this: If I call this method more than once for the same DLL, do I get handles to different insta
If the DLL is already loaded, LoadLibrary
will simply return the address of the library in memory. However, DllMain
is not called again with DLL_PROCESS_ATTACH
when the second load is attempted. Handles in the sense of libraries are just memory locations, so the value you get the second time around should be the same as the first.
As far as linux SO files go, I don't see why they would load twice either. However, someone else will have to weigh in on this to give you a proper answer.
The MSDN documentation states:
The system maintains a per-process reference count on all loaded modules. Calling LoadLibrary increments the reference count. Calling the FreeLibrary or FreeLibraryAndExitThread function decrements the reference count. The system unloads a module when its reference count reaches zero or when the process terminates (regardless of the reference count).
So it would appear that loading the module more than once (without matching calls to FreeLibrary) will return the same handle.
For Linux shared objects, from the dlopen(3) manpage:
If the same library is loaded again with
dlopen()
, the same file handle is returned. Thedl
library maintains reference counts for library handles, so a dynamic library is not deallocated untildlclose()
has been called on it as many times asdlopen()
has succeeded on it. The_init()
routine, if present, is only called once. But a subsequent call withRTLD_NOW
may force symbol resolution for a library earlier loaded withRTLD_LAZY
.