tags:

views:

286

answers:

6
+5  Q: 

What is a dll?

This may be a very noobie question, but in today's world of web app development many programmers don't need to deal with dll's much, and thus don't bother to learn about their purpose.

So what is a dll?

  1. What is it used for?
  2. How does it work?
  3. How do you create one?
  4. In what situations is creating one appropriate?

I've been told that dll's are used to store libraries of functions, but beyond that I don't know much. Hopefully someone here can enlighten me so I can finally stop wondering what all those .dll files in my Windows directory are doing.

+5  A: 

The Wikipedia page on DLLs has pretty much all the information you want.

Cody Brocious
Had to give this one a -1 because some other answers here showed more effort. If this guy doesn't know how to use wikipedia then there's no hope, anyway.
Outlaw Programmer
I do know how to use wikipedia, but preferring to ask on here rather than researching on wikipedia is the same as preferring to ask a professor rather than look it up in the book. Sometimes the professor can provide you with more insight, imho.
Daniel
A: 

Dynamically Linked Library.

To give you an example, If you have someone else's DLL loaded into you application you can use bits of programming from it.

You can load a DLL that generates random numbers that always start with "5" or something.

In you program you can call CrazyDLL.GenerateRandomNumbersSorta() and it will return the number.

For a real world example, I have DLL that combines 4 textboxes (you'd use these to type IP addresses) and it automatically only accepts numbers less than 256, and handles pressing the backspace key to jump to a previous textbox.

I've created a DLL with that code, and now all I have to do is drag and drop more of those IP address textbox collections without having to duplicate all that code over and over again.

The same DLL also has function for converting IP addresses to hexadecimal strings, and other useful code.

Grant
I've fixed it...
Omar Kooheji
+1  A: 

From MSDN Library:

A dynamic-link library (DLL) is a module that contain functions and data that can be used by another module (application or DLL).

Nemanja Trifunovic
+1  A: 

DLL = Dynamic Load Link Library. As you have been told, it is basically a collection of functions, C++ classes, and/or global variables. You can load the DLL statically (i.e. the OS loads it automatically when your program starts) or dynamically (your program explicitly loads it), at which point the functions and stuff inside the DLL are available to your program.

Creating one is similar to creating an EXE, except there doesn't need to be a main() function. There are linker directives to tell the linker to create a DLL rather than an EXE.

The main reason you'd want to do this is to encapsulate some code in one place and use it from multiple exe's, rather than linking the code into each one.

A somewhat historical reason is that your exe can be smaller since some of the code is physically located in a different file. This means that the amount of space taken up in memory by your exe can be smaller. On modern systems, this is less of an issue than it used to be, though it still might be an issue on Windows Mobile.

Graeme Perrow
+4  A: 

A DLL is a dynamic link library. It is a collection of code and/or data, which may be used by several applications (or other libraries/modules).

So for instance common methods to process files, work with GUI components etc. are made available in libraries so several applications may use the same functionality. This not only reduces the need to build the same stuff multiple times, but it also ensures that e.g. common dialogs are the same between applications.

Libraries can be loaded at runtime and thus shared between different concurrent applications. This is called dynamic linking.

In some cases the library can be included within the application itself. This is known as static linking. Static linking makes deployment easier at the cost of flexibility as different application will each load the same copy of the DLL.

However, static linking is not always an option. E.g. you can't statically link a .NET application. The user must have the .NET libraries in order to run a .NET application and libraries (or assemblies as they are called in .NET) are loaded at runtime.

DLLs are created by the same tools used to create applications. The specific details depend very much on the tools used.

Brian Rasmussen
Thanks for the answer, one thing I'm wondering is how do applications know what functions are available in the dll? The dll itself is all machine code right? So how are the public function signatures in the dll exposed?
Daniel
I don't know about most languages, but Visual Studio's auto complete will show you all the available functions.
Grant
But how does autocomplete find out what the available functions are if the dll is just machine code?
Daniel
Machines can read machine code. DLLs can contain icons, strings, fonts. Pesumably there is a list of what it contains somewhere in there.
Grant
Hmm, okay, so I guess it's contents are defined in something like a header section in the file. That makes sense, thanks.
Daniel
Visual C++ contains a tool called dumpbin -- running "dumpbin /exports whatever.dll" will tell you what functions and stuff are exported (i.e. are available for use by programs that load the DLL).
Graeme Perrow
+1  A: 

DLL = Dynamic Link Library

The name is actually quite descriptive of what they accomplish.

Library

Lets you isolate code for a specific problem domain into a single location. Then share this among multiple applications. The library can be swapped out for another at any time to fix bugs or add functionality.

Link

You can "Link" the library to an application so that the logic in the library is not compiled directly into the application.

Dynamic

The library can be loaded on-demand. Instead of loading a mammoth single EXE into memory, the OS can load only the portions needed. Plus if a DLL is shared between applications, the OS can optimize how the library is loaded and share it between apps.

Paul Alexander