Dynamic Link Libraries Inside-Out - What Does the DLL Contain?
(Page 3 of 9 )
The DLLs usually contain code, data and resources. Code is stored on read-only sections so that it can be shared among the requesting applications, but the case with data in the DLLs is different. Each requesting application gets its private copy of data segments unless one explicitly marks them as shared. There may be resource-only DLLs too that we’re going to discuss shortly.
The code section contains classes or standalone (unrelated) functions that you want to expose through the DLL. In case of classes you’ve all the functionality and data bound into one entity already, but with standalone related functions you may have some shared global data too. All of this is defined in one or more source files but finally packaged as one DLL. Classes and functions that are available to the applications using the DLL are called exports from the DLL. If your DLL uses functions from other DLLs, then those are called as imports to the DLL. A free useful utility that allows you view exported and imported functions from a DLL or EXE file is freely available from www.dependencywalker.com. Below is a snapshot of the same displaying exports from the WinScard.dll.

Figure 1: Function Exports of a Windows System DLL
Exporting Classes and Functions from the DLL
To make the code you’ve compiled in a DLL, you must explicitly export it so that other applications may use it. There are two ways to do so:
- By creating a module definition (.DEF) file and use the .DEF file when building the DLL. This approach also facilitates exporting functions by ordinal rather than by name, which is the default. You’ll need to specify the /DEF while you use the linker to build the DLL.
- By using the keyword __declspec (dllexport) in the function's definition. In case you wish to export classes, you’ll place this keyword after the class keyword. A complier specific name mangling technique is used to mangle member function names and other exports. If you specify C linkage by using the extern “C” keyword, then name mangling won’t be performed.
Let's say you’ve a function Foo(Type1 a,Type2 b) and you wish to export it from the DLL you’re writing, so you just append __declspec(dllexport) keyword before the function name or write a module definition file with the following contents:
LIBRARY FooLib
EXPORTS
Foo private @1
Here the last line tells the linker (you’ve to do so with a /DEF switch) to export the function.
Next: The DLL Entry Point Function: DllMain >>
More Code Examples Articles
More By Digvijay Chauhan