views:

1047

answers:

8
+4  Q: 

DLL information

How exactly do dlls work? There seems to be an awful lot of them, but I don't know what they are or how they work.

So, what's the deal with them?

+15  A: 

What is a DLL?

Dynamic Link Libraries (DLL)s are like EXEs but they are not directly executable. They are similar to .so files in Linux/Unix. That is to say, DLLs are MS's implementation of shared libraries.

DLLs are so much like an EXE that the file format itself is the same. Both EXE and DLLs are based on the Portable Executable (PE) file format. DLLs can also contain COM components and .NET libraries.

What does a DLL contain?

A DLL contains functions, classes, variables, UIs and resources (such as icons, images, files, ...) that an EXE, or other DLL uses.

Types of libraries:

On virtually all operating systems, there are 2 types of libraries. Static libraries and dynamic libraries. In windows the file extensions are as follows: Static libraries (.lib) and dynamic libraries (.dll). The main difference is that static libraries are linked to the executable at compile time; whereas dynamic linked libraries are not linked until run-time.

More on static and dynamic libraries:

You don't normally see static libraries though on your computer, because a static library is embedded directly inside of a module (EXE or DLL). A dynamic library is a stand-alone file.

A DLL can be changed at any time and is only loaded at runtime when an EXE explicitly loads the DLL. A static library cannot be changed once it is compiled within the EXE. A DLL can be updated individually without updating the EXE itself.

Loading a DLL:

A program loads a DLL at startup, via the Win32 API LoadLibrary, or when it is a dependency of another DLL. A program uses the GetProcAddress to load a function or LoadResource to load a resource.

Further reading:

Please check MSDN or Wikipedia for further reading. Also the sources of this answer.

Brian R. Bondy
Should probably mention the import lib somewhere. OK I'll go away now. :)
Adam Mitz
Thanks for the comments adam, corrected most.
Brian R. Bondy
Removed those that seem to be addressed. How do I get points for this? <g>
Adam Mitz
+1  A: 

DLLs (Dynamic Link Libraries) contain resources used by one or more applications or services. They can contain classes, icons, strings, objects, interfaces, and pretty much anything a developer would need to store except a UI.

tsilb
They can actually store a UI and several programs do this. For example snap-ins.
Brian R. Bondy
+1  A: 

http://support.microsoft.com/kb/815065

A DLL is a library that contains code and data that can be used by more than one program at the same time. For example, in Windows operating systems, the Comdlg32 DLL performs common dialog box related functions. Therefore, each program can use the functionality that is contained in this DLL to implement an Open dialog box. This helps promote code reuse and efficient memory usage.

By using a DLL, a program can be modularized into separate components. For example, an accounting program may be sold by module. Each module can be loaded into the main program at run time if that module is installed. Because the modules are separate, the load time of the program is faster, and a module is only loaded when that functionality is requested.

Additionally, updates are easier to apply to each module without affecting other parts of the program. For example, you may have a payroll program, and the tax rates change each year. When these changes are isolated to a DLL, you can apply an update without needing to build or install the whole program again.

http://en.wikipedia.org/wiki/Dynamic-link_library

smink
+2  A: 

Here's some links to some info about DLLs:

Wikipedia

MSDN

itsmatt
+3  A: 

DLLs contain an Export Table which is a list of symbols which can be looked up by the calling program. The symbols are typically functions with C calling convention (__stcall). The export table also contains the address of the function.

With this information, the calling program can then call the functions within the DLL even though it did not have access to the DLL at compile time.

Here's an article with some more information

Adam Pierce
+1  A: 

DLLs (dynamic link libraries) and SLs (shared libraries, equivalent under UNIX) are just libraries of executable code which can be dynamically linked into an executable at load time.

Static libraries are inserted into an executable at compile time and are fixed from that point. They increase the size of the executable and cannot be shared.

Dynamic libraries have the following advantages:

1/ They are loaded at run time rather than compile time so they can be updated independently of the executable (all those fancy windows and dialog boxes you see in Windows come from DLLs so the look-and-feel of your application can change without you having to rewrite it).

2/ Because they're independent, the code can be shared across multiple executables - this saves memory since, if you're running 100 apps with a single DLL, there may only be one copy of the DLL in memory.

Their main disadvantage is advantage #1 - having DLLs change independent your application may cause your application to stop working or start behaving in a bizarre manner. DLL versioning tend not to be managed very well under Windows and this leads to the quaintly-named "DLL Hell".

paxdiablo
+1  A: 

Let’s say you are making an executable that uses some functions found in a library.

If the library you are using is static, the linker will copy the object code for these functions directly from the library and insert them into the executable.

Now if this executable is run it has every thing it needs, so the executable loader just loads it into memory and runs it.

If the library is dynamic the linker will not insert object code but rather it will insert a stub which basically says this function is located in this DLL at this location.

Now if this executable is run, bits of the executable are missing (i.e the stubs) so the loader goes through the executable fixing up the missing stubs. Only after all the stubs have been resolved will the executable be allowed to run.

To see this in action delete or rename the DLL and watch how the loader will report a missing DLL error when you try to run the executable.

Hence the name Dynamic Link Library, parts of the linking process is being done dynamically at run time by the executable loader.

One a final note, if you don't link to the DLL then no stubs will be inserted by the linker, but Windows still provides the GetProcAddress API that allows you to load an execute the DLL function entry point long after the executable has started.

jussij
+2  A: 

What is a DLL

DLL's are binary files that can contain executable code and resources like IMAGES etc. Unlike appications these cannot be directly executed but an application will load them as and when they are required (or all at once during startup)

Are they important

Most applications will load the dlls it require at startup. If any of these are not found the system will not be able to start the process at all.

DLLs might require other DLLs

In the same way that an application requires a DLL, a DLL might be dependent on other DLL's itself. If one of these DLLs in the chain of dependency is not found, the application will not load. This is debugged easily using any Depedency walker tools. http://www.dependencywalker.com/

There are so many of them in the system folders

Most of the system functionality is exposed to user program in the form of DLL's as they are a standard form of sharing code / resources. Each functionality is kept seperately in different dlls so that only the required DLLs will be loaded and thus reduce the memory constraints on the system.

Installed applications also use DLLs

DLLS also becomes a form of separating functionalities physically as explained above. Good applications also try to not load the dlls until they are absolutely required, which reduces the memory requirements. This too causes apps to ship with a lot of DLLs.

Dll Hell

However at times system upgrades often breaks other programs when there is a version mismatch between the shared dlls and the program that require them. System checkpoints and dll cache etc have been the initiatives from M$ to solve this problem. _NET platform might not face this issue at all.

How do we knows whats inside a dll

You have to use an external tool like Dumpbin or Dependency walker which will not only show what publically visible functions (known as exports) are contained inside the DLL's and also what other DLLs it requires and which exports from those DLL's this DLL is dependent upon.

How do we create / use them

Refer the programming documentation from your vendor For C++ refer LoadLibrary in MSDN.