1

I was reading about it and the part about memory was really confusing. What exactly happens to a DLL after compilation?

Questions that were somewhat bugging me:

Is it loaded into memory only once and all processes that require access to it are given only a pointer to where it is?

When is it loaded? I am sure it isn't just arbitrarily loaded into memory after compilation so is there a special procedure to load it or does Windows load a DLL when a process requires it and keeps it for sharing among other processes?

From the Microsoft docs

Every process that loads the DLL maps it into its virtual address space. After the process loads the DLL into its virtual address, it can call the exported DLL functions.

How does that "mapping" look like? I found that a bit confusing.

I don't know if this is a relevant piece of info but I am specifically interested in custom DLLs (DLLs written by me), not system DLLs

  • When DLLs were first invented, they were meant to be shared between different processes. I don't think that's the case any more, but I don't know which version of Windows it changed in or where it's documented. – Mark Ransom Dec 09 '19 at 17:55
  • @MarkRansom wait is it not that way anymore? What's the case now then? I am on win10. –  Dec 09 '19 at 18:01
  • I think the whole DLL is loaded again for each process. But again I can't remember how I got that idea, so don't take it as gospel unless I or someone else can back it up with documentation. – Mark Ransom Dec 09 '19 at 18:11

1 Answers1

0

An exe file lists DLLs is wants to link to, so when the loader loads an exe, it loads DLLs that are listed as required, unless they are already loaded. A DLL may run initialization code the first time it loads.

Of course a DLL can be loaded dynamically by name, then it's loaded when the LoadLibrary API call is issued by a program. This is useful to implement dynamically loadable plugins.

Windows keeps a reference counter for each DLL, so when all processes stopped referencing a DLL, either by exiting or explicitly calling FreeLibrary, Windows will unload the DLL, giving it a chance to run any cleanup code.

9000
  • 39,899
  • 9
  • 66
  • 104