DLLs: An Overview
A dynamic-link library (DLL) is an executable file that acts as a shared library of functions. Dynamic linking provides a way for a process to call a function that is not part of its executable code. The executable code for the function is located in a DLL, which contains one or more functions that are compiled, linked, and stored separately from the processes that use them. DLLs also facilitate the sharing of data and resources. Multiple applications can simultaneously access the contents of a single copy of a DLL in memory. This code can be called from a PowerBuilder program even though it is written in another language such as C/C++, Pascal or in any other language that supports the pascal, WINAPI macro calling sequence for 16-bit and the standard calling sequence for 32-bit.
Dynamic linking differs from static linking in that it allows an executable module (either a .DLL or .EXE file) to include only the information needed at run time to locate the executable code for a DLL function. In static linking, the linker gets all the referenced functions from the static link library and places it with your code into your executable. Using DLLs instead of static link libraries makes the size of the executable file smaller. If several applications use the same DLL, this can be a big savings in disk space and memory.
A .DLL file has a layout very similar to an .EXE file, with one important difference - a DLL file contains an exports table. The exports table contains the name of every function that the DLL exports to other executables. These functions are the entry points into the DLL; only the functions in the exports table can be accessed by other executables. Any other functions in the DLL are private to the DLL.
You can export functions from a DLL with two methods:
1) Create a module definition (.DEF) file and use the .DEF file when building the DLL.
2) Use the keyword __declspec(dllexport) in the function's definition. When exporting functions with either method, make sure to use the __stdcall calling convention.
The __stdcall calling convention is used to call Win32 API functions.
__stdcall pushes parameters on the stack, in reverse order (right to left)
Functions that use this calling convention require a function prototype. The following list shows the implementation of this calling convention.
Element ImplementationArgument-passing order Right to left.
Argument-passing convention By value, unless a pointer or
reference type is passed.
Stack-maintenance responsibility Called function pops its own
arguments from the stack.
Name-decoration convention An underscore (_) is prefixed to the
name. The name is followed by the at
sign (@) followed by the number of
bytes (in decimal) in the argument list.
Therefore, the function declared as
int func( int a, double b ) is decorated
as follows: _func@12
Case-translation convention None
Example
In the following example, use of __stdcall results in all WINAPI function types being handled as a standard call:
// Example of the __stdcall keyword
#define WINAPI __stdcall
Export from a DLL Using .DEF Files
The most common methods for exporting a definition:
1.The __declspec(dllexport) keyword in the source code
2.An EXPORTS statement in a .DEF file
You can export data, functions, classes, or class member functions from a DLL by using the __declspec(dllexport) keyword. If you use __declspec(dllexport), you do not need a .DEF file for exports.
To export functions, the __declspec(dllexport) keyword must appear to the left of the calling-convention keyword, if a keyword is specified. For example:
void __declspec(dllexport) __cdecl Function1(void);
To export all the public data members and member functions in a class, the keyword must appear to the left of the class name as follows:
class __declspec(dllexport) CExampleExport : public CObject
{ ... class definition ... };
When building your DLL, you typically create a header file that contains the function prototypes and/or classes you are exporting, and add the __declspec(dllexport) to the declarations in the header file. To make your code more readable, define a macro for __declspec(dllexport) and then use the macro with each symbol you are exporting:
#define DllExport __declspec( dllexport )
Pros and Cons of Using __declspec(dllexport)
Using __declspec(dllexport) is convenient because you do not have to worry about maintaining a .DEF

