Download the DLL more than once?

I am using the LoadLibrary function to load a DLL on Windows. My question is this: if I call this method more than once for the same DLL, can I get descriptors for different instances of the DLL, or will they all refer to the same instance?

Also, how does this behavior correlate with Linux SO files, is it the same or completely different, and what assumptions can I make in this regard? Thanks.

+6
source share
3 answers

The MSDN documentation states:

The system supports reference counting for each process for all loaded modules. Calling LoadLibrary increases the reference count. the calling function of FreeLibrary or FreeLibraryAndExitThread decreases the reference count. The system unloads the module when its reference counter reaches zero or when the process terminates (regardless of the reference counter).

Thus, it would turn out that loading the module more than once (without corresponding calls in FreeLibrary) will return the same descriptor.

+11
source

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 trying a second load. Pens in the sense of libraries are only memory cells, so the value you get the second time should be the same as the first.

As for linux SO files, I don't understand why they will be downloaded twice. However, someone else will have to weigh this to give you the correct answer.

+3
source

For shared Linux objects, from the dlopen(3) manpage :

If the same library is loaded again with dlopen() , the same file is returned . The dl library supports reference counting for the library, so the dynamic library is not freed until dlclose() is called on it as many times as dlopen() has succeeded . The _init() routine , if any, is called only once . But a subsequent call with RTLD_NOW may result in character resolution for the library previously loaded with RTLD_LAZY .

+2
source

Source: https://habr.com/ru/post/907918/


All Articles