Win32 DLL



A Win32 DLL is a library that can be made available to programs that run on a Microsoft Windows computer. As a normal library, it is made of functions and/or other resources grouped in a file. The DLL abbreviation stands for Dynamic Link Library. This means that, as opposed to a static library, a DLL allows the programmer to decide on when and how other applications will be linked to this type of library. For example, a DLL allows difference applications to use its library as they see fit and as necessary. In fact, applications created on different programming environments can use functions or resources stored in one particular DLL. For this reason, an application dynamically links to the library.

You create a DLL like any other application, as a project. This project must contain at least one file. From your experience, you probably know that each C++ program contains the main() function and each Win32 application contains a WinMain() function. These are referred to as entry-points because the compiler always looks for these functions as the starting point of an application. In the same way, a Win32 DLL must contain an entry point function. The most regularly used function as the entry point is called DllMain. Unlike a C++ program that uses main() and unlike a Win32 application that uses WinMain(), a DLL can have a different function as the entry-point but it must have an entry point. When building your DLL, you will need to communicate your entry-point to the compiler.

When creating the DLL, you must provide a way for external applications to access the functions or resources included in the DLL. There are two main ways you will do this: using a statement that indicates that a particular function will be exported, or by creating an additional specially configured file that will accompany the DLL.

Fundamentals of a DLL

We have mentioned that a DLL is created as a project that contains at least one source file and this source file should present an entry-point. After creating the DLL, you will build and distribute it so other programs can use it. When building it, you must create a library file that will accompany the DLL. This library file will be used by other programs to import what is available in the DLL:

When the import library is created, it contains information about where each available function is included in the DLL and can locate it. When an application needs to use a function contained in the DLL, it presents its request to the import library. The import library checks the DLL for that function. If the function exists, the client program can use it. If it doesn't, the library communicates this to the application and the application presents an error.

Normally, the import library is created when building the DLL but you must provide a mechanism for the compiler to initiate it. There are two main ways you do this. You can use a calling convention that allows the compiler to detect what function will be accessible to the clients of the DLL. In this case, each one of these functions will start with the _declspec modifier. When creating the DLL, the _declspec modifier must take as argument the dllexport keyword.

The functions do not have to be included in the main file that contains the entry-point. It may simply be convenient to do it that way.

Practical Learning: Creating a Win32 DLL

  1. To start a new project, on the main menu, click File -> New -> Project…
  2. In the New Project dialog box, in the Project Type list, select Visual C++. In the Templates list, click Win32 Project
  3. In the Name edit box, type MomentOfInertia
  4. Click OK
  5. In the Win32 Application Wizard, in the left frame, click Application Settings. In the Application Type section, click the DLL radio button
  6. Click Finish
  7. Change the contents of the MomentOfInertia.cpp file as follows:
    // MomentOfInertia.cpp : Defines the entry point for the DLL application.
    #include "stdafx.h"
    // Calculation of the moment of inertia
    // Rectangle
    extern "C" _declspec(dllexport)double MOIRectangle(double b, double h);
    // Semi-Circle
    extern "C" _declspec(dllexport)double MOISemiCircle(double r);
    // Triangle
    extern "C" _declspec(dllexport)double MOITriangle(double b, double h, int);
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
        return TRUE;
    double MOIRectangle(double b, double h)
    	return b * h * h * h / 3;
    double MOISemiCircle(double r)
    	const double PI = 3.14159;
    	return r * r * r * r * PI / 8;
    double MOITriangle(double b, double h, int)
    	return b * h * h * h / 12;
  8. To create the DLL, on the main menu, click Build -> Build MomentOfInertia
  9. Open Windows Explorer and open the Debug folder of the current project. Notice that a file with dll extension and another file with lib extension have been created

Win32 DLL Test

Naturally you should test your DLL to make sure it behaves as expected, before distributing it to other applications. For an application to be able to use the DLL, it must be able to locate its dll and its lib files. The simplest way to do this is to simply copy and paste these files in the client project. Some other times, the DLL and its library will need to reside in the system directory. To make the DLL available to an application, you must import the DLL into the application. Although there are various ways to do this, the simplest consists of adding it to the project by using the main menu where you would click Project -> Add Existing Item…, and selecting the lib file.

We mentioned that, when creating the DLL, you should use the _declspec(dllexport) modifier for each function that will be accessed outside of the DLL. In the project that is using the DLL, each function that will be accessed must be declared using the _declspec(dllimport) modifier.

Practical Learning: Testing a DLL

  1. To start a new project, display the New Project dialog box
  2. In the Project Types, click Visual C++ Projects. In the Templates, click MFC Application
  3. In the Name edit box, type MOITest and press Enter
  4. In the MFC Application Wizard, create the application as Dialog Based without an About Box and set the Dialog Title to Moment Of Inertia DLL Test
  5. Click Finish
  6. Open Windows Explorer or My Computer. Locate the folder that contains the MomentOfInertia project
  7. Select and copy both the MomentOfInertia.dll and the MomentOfInertia.lib files
  8. Locate the folder that contains the current new project then paste the dll and the lib files:
  9. Back in MSVC, on the main menu, click Project -> Add Existing Item…
  10. In the Add Existing Item dialog box, change the Files of Type to All Files
  11. In the list of files, click MomentOfInertia.lib
  12. Click Open
  13. Delete the TODO line on the dialog box. Add a new Button. Change its Caption to MOI Test
  14. Double-click the new button to initiate its BN_CLICKED message
  15. Implement it as follows:
    // MOITestDlg.cpp : implementation file
    #include "stdafx.h"
    #include "MOITest.h"
    #include "MOITestDlg.h"
    #include ".\moitestdlg.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    // Calculation of the moment of inertia
    // Rectangle
    extern "C" _declspec(dllimport)double MOIRectangle(double b, double h);
    // Semi-Circle
    extern "C" _declspec(dllimport)double MOISemiCircle(double r);
    // Triangle
    extern "C" _declspec(dllimport)double MOITriangle(double b, double h, int);
    // CMOITestDlg dialog
    CMOITestDlg::CMOITestDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CMOITestDlg::IDD, pParent)
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    . . .
    void CMOITestDlg::OnBnClickedButton1()
    	// TODO: Add your control notification handler code here
    	double MOI = MOIRectangle(3.25, 2.65);
    	char Sentence[40];
    	sprintf(Sentence, "Moment of Inertia: %.3f", MOI);
  16. Test the application

Previous Copyright © 2004-2006 FunctionX, Inc. Next