A Windows DLL (Dynamic-Link Library) is very similar in nature to a Unix dynamic (shared object) library. A DLL contains compiled functions that can be reused by many different applications.

To create a Win32 DLL project, select the New option from the File menu, or press [Ctrl-n]. The following dialog will appear:

New Project Dialog

Choose Win32 Dynamic-Link Library from the project types list, specify the project name and the location on disk for the application. If a workspace is already open, choose whether to create a new workspace or to add this new project to the current workspace. After setting up the basic project information and pressing the OK button, another dialog will appear:

New DLL Dialog

Selecting An empty DLL project will create a new project that contains no files. Now we can create new files to add to the project or insert pre-written files into the project. This is accomplished using the same steps that were described in the section on Creating a Console Application.

In order to build a DLL, a module-definition file must be added to the project. The module-definition file, which has the file extension .def, lists the DLL functions that should be callable from a client application. For example, if the source code defines functions called foo, bar, and baz, the following module-definition file would specify that only foo and baz should be externally callable:

;MyLibrary.def
;The following statement tells the linker to build a 
;DLL called MyLibrary.dll
LIBRARY "MyLibrary"
;The following line declares an exports section.
EXPORTS
  foo
  baz

Comment lines begin with ;. The foo and baz function names appear in the exports section to indicate that a client program should be allowed to call these functions. The bar function name is omitted from the exports section to indicate that this function can only be called by functions defined in the DLL's source code.

To generate the DLL outputs, select the Build option from the Build menu on the menu bar. Note that there two significant output files created when building a DLL: a .dll file and a .lib file. The .dll file contains the compiled functions that were implemented in the project source code. The .lib file is known as an import library. An import library is used when linking a program that makes calls to the functions exported by a DLL. The import library provides information to the linker about the function signatures and function locations within the compiled DLL code.

In order for a client application to use the DLL that has been created, the following files must be supplied to the client application programmer:

The header files, the .lib file, and the .dll file should be copied into the project directory of the client application. Add the .h and .lib files to the client application project using the steps described in the section on Creating a Console Application.

In order to use the functions defined in the DLL, the source code in the client application should include the header file(s) copied from the DLL project. After the client project is compiled and linked, it can be executed by pressing [Ctrl-F5]. When the application is loaded by the operating system, the operating system will automatically load the necessary DLL file.

In order to debug a DLL, a client application must first be created to exercise the functions supplied by the DLL. This is because a DLL does not contain a main function that can be executed directly.

To begin debugging a DLL, first open the workspace containing the DLL project. Select and build the debug configuration of the DLL project. When the build is complete, copy the header files, .lib file, and the .dll file to the project directory of the client application. Next, open the workspace containing the client application. Select and build the debug configuration of the client application. Finally, set breakpoints in the client application source code on the lines that need to be debugged. When debugging a statement that contains a call to a DLL function, it will now be possible to step into the source code for the DLL function.