Link to home
Start Free TrialLog in
Avatar of gklux
gklux

asked on

about DLL loading

2 programs are using the same DLL.

Question 1 is :
It there are both launched, how many times the DLL is loaded in memory ? once or twice ?

Question2 is :
If One program is loaded twice. how many times the DLL is loaded in memory ? once or twice ?

regards.
Mike
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

DLL is loaded each time LoadLibrary() is called.
ziolko.
ASKER CERTIFIED SOLUTION
Avatar of Epsylon
Epsylon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of article
article

The DLL will be loaded only once.  That's why one use DLL to share common codes.
:o/
as far as i know a dll is allways loaded once, and multiple loads are counted by an referncecounter.

only the memory for vars/structures is
allocated additional for each processwell, i'm not an expert of this,
therefore i may wrong

meikl ;-)
Epsylon's link should explain it all in detail, but in case you're too lazy to read all that stuff, here the answer in short:

"article"'s answer is wrong. Each process loads its own copies of each needed dll. A prove is simple: You can change segments of a dll in the memory context of your own process - and this "patching" shows only effect in the current process. Several API hooking methods are using this effect, e.g. ImportTablePatching works only process wide.

However, there are some exceptions to what I said above:

(1) In win9x based systems there is a shared area ($80000000 - $FFFFFFFF) and a non-shared area ($00000000 - $7FFFFFFFF). The non-shared area is private to each process. Dlls loaded there are also private to each process. Dlls that are loaded in the shared area (some system dlls) are shared among all processes. Those dlls are only loaded once for all processes. In winNT based systems the whole memory area is private, there is no shared area, thus no shared dlls, either.
(2) If some segments of a dll are marked as being shared, these segments are mapped into all processes by Windows.
(3) To boost up the overall speed, Windows internally does a lot of tricks to avoid double loading of dlls system wide. But that's burried in the deepest OS regions and hidden very well. Technics like CopyOnWrite and such stuff help realizing this. But at application level it is really this way, that each process has its own copy of all (non-shared) dlls.

Regards, Madshi.
as i said, i'm not an expert for this :-)
Epsylon's comment confirms what I said earlier: each time LoadLibrary() is called.
ziolko.
Hi ziolko,

if the same process calls LoadLibrary twice, the dll is only loaded once and a reference count is incremented. Furthermore dlls are also loaded if they're statically linked to by another module, without that anyone calls LoadLibrary.

Regards, Madshi.
Avatar of gklux

ASKER

Hi all, and thanks for such lot answers.

I Think that Epsylon's answer is the best one.

best regards.

Mike
Avatar of gklux

ASKER

thanks and regards
Mike
thank you too, Mike
Madshi thx I didn't know that.
ziolko.