DLL Module-Definition Files

 

Definition File Fundamentals

We mentioned that a dll must provide a means of importing its functions and making them available to client applications. We learned above how to help the compiler create the import library by preceding at least one function with the _declspec(dllexport) modifier. Microsoft Windows (I don't know if this technique is available on Linux although I know Linux also uses DLLs) allows another technique. Instead of preceding your functions with a modifier, you can instead add another file called the Module-Definition file.

A definition file is a text file that has the extension def. It must contain at least two sections. The first section is made of one line. It starts with the LIBRARY word followed by the name of the DLL. It is important that the name you specify be the same name as the DLL that will be made available to other applications. The second section starts with the EXPORTS word and contains a list of the functions that will be exported.

In the following exercise, we will create a DLL that can be used to calculate the moment of inertia using the following illustrations and formulas:
Rectangle
Semi-Circle
Triangle

The moment of inertia is taught in technical high school in Mechanical Engineering or in the first two years of university in Engineering Science. For the following exercises, you don't need to know anything about the moment of inertia. We will simply apply the above formulas.

Practical Learning: Creating an MFC Static Library

  1. To start a new application, open the New dialog box (File -> New...) and, in the Projects property page, click Win32 Dynamic-Link Libraries. In the Project Name box, type MomentInertia and press Enter
  2. In the Win32 Dynamic-Link Library Step 1 of 1, make sure the An Empty DLL Project radio button is selected.
    Click Finish then click OK
  3. To add a header file, on the main menu, click File -> New…
  4. In the New dialog box and in the Files property page, click C/C++ Header File
  5. In the Name edit box, type stdafx and press Enter
  6. In the empty file, type the following:
     
    #ifndef STDAFX_H_
    #define STDAFX_H_
    
    #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
    
    #include <windows.h>
    
    #endif // STDAFX_H_
  7. To add the accompanying source file, display the New dialog box and select C++ Source File
  8. In the Name edit box, type stdafx and press Enter
  9. In the empty file, type the following:
     
    #include "stdafx.h"
  10. To add a new source file, on the main menu, click File -> New...
  11. In the Name edit box, type MomentInertia and press Enter
  12. In the empty file, type the following:
     
    #include "stdafx.h"
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                                           DWORD  ul_reason_for_call, 
                                           LPVOID lpReserved )
    {
        return TRUE;
    }
  13. To add a header file, on the main menu, click File -> New…
  14. In the New dialog box and in the Files property page, click C/C++ Header File
  15. In the Name edit box, type MomentOfInertia and press Enter
  16. In the empty file, type the following:
     
    // MomentOfInertia.h : Defines the entry point for the DLL application.
    //
    #pragma once
    
    // Calculation of the moment of inertia
    // Rectangle
    double MOIRectX(double b, double h);
    double MOIRectY(double b, double h);
    double MOIRectXC(double b, double h);
    double MOIRectYC(double b, double h);
    
    // Circle
    double MOICircleXC(double r);
    double MOICircleYC(double r);
    
    // Semi-Circle
    double MOISemiCircleX(double r);
    double MOISemiCircleXC(double r);
    double MOISemiCircleYC(double r);
    
    // Triangle
    double MOITriangleX(double b, double h, int);
    double MOITriangleXC(double b, double h, int);
  17. To add the accompanying source file, display the New dialog box and select C++ Source File
  18. In the Name edit box, type MomentOfInertia and press Enter
  19. In the empty file, type the following:
     
    #include "stdafx.h"
    #include "MomentOfInertia.h"
    
    const double PI = 3.14159;
    
    // Rectangle
    double MOIRectX(double b, double h)
    {
    	return b * h * h * h / 3;
    }
    double MOIRectY(double b, double h)
    {
    	return h * b * b * b / 3;
    }
    double MOIRectXC(double b, double h)
    {
    	return b * h * h * h / 12;
    }
    double MOIRectYC(double b, double h)
    {
    	return h * b * b * b / 12;
    }
    
    // Circle
    double MOICircleXC(double r)
    {
    	return PI * r * r * r * r / 4;
    }
    double MOICircleYC(double r)
    {
    	return PI * r * r * r * r / 4;
    }
    
    // Semi-Circle
    double MOISemiCircleX(double r)
    {
    	return r * r * r * r * PI / 8;
    }
    double MOISemiCircleXC(double r)
    {
    	return 0.110 * r * r * r * r;
    }
    double MOISemiCircleYC(double r)
    {
    	return MOISemiCircleX(r);
    }
    
    // Triangle
    double MOITriangleX(double b, double h, int)
    {
    	return b * h * h * h / 12;
    }
    double MOITriangleXC(double b, double h, int)
    {
    	return b * h * h * h / 36;
    }
  20. To add a definition file, display the New dialog box again
  21. In the Files property page, click Text File
  22. In the Name edit box, type "MomentInertia.def" and press Enter (the double-quotes are used so the file would be saved with the def extension instead of txt; if you forget to use the double-quotes, you can still save the file as (File -> Save As...) a def file)
  23. In the empty file, type the following:
     
    LIBRARY	MomentInertia
    
    EXPORTS
    	MOIRectX
    	MOIRectY
    	MOIRectXC
    	MOIRectYC
    
    	MOICircleXC
    	MOICircleYC
    
    	MOISemiCircleX
    	MOISemiCircleXC
    	MOISemiCircleYC;
    
    	MOITriangleX
    	MOITriangleXC
  24. To create the DLL, on the main menu, click Build -> Build MomentInertia.dll
 

Usage of a Definition File DLL

Although we mentioned that you can create a definition file to have an import library, you don't have to use that definition file, although you can. The technique we used above allows the compiler to know that you need an import library. This means that there are at least two different ways you can make use of a definition file (as we stated already, the most difficult aspect of a DLL is based on decisions, not how to create it).

Once again, you can use the dll and lib files in the client applications of your DLL, as we did already. If you don't want to use the definition file, you must provide a mechanism for a client application to locate the function that is included in your DLL. This time also, there are different ways you can do this. When a client application wants to use a function, it can declare the function in the file that will call the function. The declaration is done as a normal C++ function declaration. The function must be listed (exactly) as it appears in the DLL. This is why you should (strongly) always provide documentation for your DLL: other people or companies shouldn't spend time predicting or guessing what your DLL is used for or what it contains. In the following example, a function called Number() is called from main(). The function is only declared but it is not defined because this was already taken care of in a DLL:

#include <iostream>
using namespace std;

double Number();

int main()
{
	double Nbr = Number();

	cout << "Number: " << Nbr << endl;
	return 0;
}

Another technique consists of including the header file that contains the function definitions from the DLL. This means that, besides the dll and the lib files, if you distribute your library, you must also distribute the header file (provided you are not distributing the def file, although you can distribute both).

 

Practical Learning: Using a Module Definition DLL

  1. To start a new application, display the New dialog box and click the Projects property page
  2. Select MFC AppWizard (exe)
  3. In the Name edit box, type MOIDefTest and press Enter
  4. In the MFC AppWizard Step 1, click Dialog Based and click Next
  5. Remove the check box of About Box and change the Dialog Title to
    DLL Definition File Test
  6. Click Finish and click OK
  7. Open Windows Explorer or My Computer. Locate the folder that contains the MomentInertia project and open its Debug folder
  8. Select and copy both the MomentInertia.dll and the MomentInertia.lib files
  9. Locate the folder that contains the current new project then paste the dll and the lib files
  10. To use the header file, in the folder that contains the MomentInertia project and copy the MomentOfInertia.h header file
  11. Paste the MomentOfInertia.h file in the folder that contains the current project
  12. Back in MSVC, on the main menu, click Project -> Add To Project -> Files…
  13. In the Add Existing Item dialog box, change the Files of Type to Library Files (.lib)
  14. In the list of files, click MomentInertia.lib
  15. Click Open
  16. Delete the TODO line on the dialog box
  17. Add a new button. Change its Caption to Moment Of Inertia
  18. Double-click the new button to initiate its BN_CLICKED message. Accept the suggested name of the function and click OK
  19. Implement it as follows:
     
    // MOIDefTestDlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "MOIDefTest.h"
    #include "MOIDefTestDlg.h"
    
    #include "MomentOfInertia.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    . . .
    
    HCURSOR CMOIDefTestDlg::OnQueryDragIcon()
    {
    	return (HCURSOR) m_hIcon;
    }
    
    void CMOIDefTestDlg::OnButton1() 
    {
    	// TODO: Add your control notification handler code here
    	double MOI = MOIRectX(3.25, 2.15);
    	char Sentence[40];
    
    	sprintf(Sentence, "Moment of Inertia: %.3f", MOI);
    	AfxMessageBox(Sentence);
    }
  20. Test the application:
     
  21. Close the dialog box
 

Previous Copyright © 2004-2006 FunctionX, Inc. Next