Win32 Static DLL
 

Introduction

A Dynamic Link Library (DLL) is a program that holds one or more functions or some functionality that other programs can use (that's the shortest definition I could come up with...). As such, you can create one DLL and use it in many programs or even distribute it to other programmers (because a DLL is usually treated as an "executable" program, you cannot e-mail it to somebody as an attachment; most e-mail programs would not carry it). Most of the time, a DLL is just a file that has the extension .dll but it could also have another extension.

The DLL we are going to create can be used in any program that uses MFC. Unfortunately, and based on my experience, you should not distribute it to people who are using other compilers such as Borland C++ Builder or Dev-C++. In reality you can. Because they will not be able to simply "plug" it in their programs, apparently there are some things they can do to make it work. I cannot cover that. In fact, the only reason I am writing this is because I can't believe how hard it is to find this information, whether in books or on the Internet; people who write books either assume that everybody knows how to create a DLL or they simply don't know, which is incredible considering, as you will see soon, how easy this thing is (I myself looked for it in many places, to no avail).

 

Creating a Project DLL

To start, we need a project that would carry the DLL.

  1. Start Microsoft Visual C++. On the main menu, click File -> New -> Project...
  2. In the New Project dialog box, in the Project Types list, click the Visual C++
  3. In the Templates list, click Win32 Project Projects tab and click Win32 Dynamic Link Library.
  4. In the Project Name, type a name such as ExoDLL and specify your desired folder in the Location box
     
  5. Click OK
  6. In the Win32 Application Wizard, click Application Settings
  7. In the Application Type section, click the DLL radio button
     
  8. Click Finish
 

Coding the DLL

To make this project a little useful for our learning process, we will use two types of functions: those that are used internally by the DLL only, and those that can be used by external functions, that is, the other programs that will need this DLL.

  1. To create functions that are used internally by the DLL, change the program as follows:
     
    // ExoDLL.cpp : Defines the entry point for the DLL application.
    //
    
    #include "stdafx.h"
    
    // This function is used to calculate the total area of a parallelepid rectangle
    double BoxArea(double L, double H, double W);
    // This function is used to calculate the volume of a parallelepid rectangle
    double BoxVolume(double L, double H, double W);
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
        return TRUE;
    }
    
    double BoxArea(double L, double H, double W)
    {
        return 2 * ((L*H) + (L*W) + (H*W));
    }
    
    double BoxVolume(double L, double H, double W)
    {
        return L * H * W;
    }
  2. Notice that the BoxArea() and the BoxVolume() functions are declared and defined just like any other function we know already.
    To declare and define a function that can be used outside of the DLL, that is, by a function that uses the DLL, change the file as follows:
     
    // ExoDLL.cpp : Defines the entry point for the DLL application.
    //
    
    #include "stdafx.h"
    
    // This function is used to calculate the total area of a parallelepiped rectangle
    double BoxArea(double L, double H, double W);
    // This function is used to calculate the volume of a parallelepiped rectangle
    double BoxVolume(double L, double H, double W);
    // This function is used to get the dimensions of a rectangular parallelepiped
    // calculate the area and volume, then pass the calculated values to the
    // function that called it.
    extern "C" __declspec(dllexport)void BoxProperties(double Length, double Height,
                                        double Width, double& Area, double& Volume);
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
        return TRUE;
    }
    
    double BoxArea(double L, double H, double W)
    {
        return 2 * ((L*H) + (L*W) + (H*W));
    }
    
    double BoxVolume(double L, double H, double W)
    {
        return L * H * W;
    }
    
    void BoxProperties(double L, double H, double W, double& A, double& V)
    {
        A = BoxArea(L, H, W);
        V = BoxVolume(L, H, W);
    }
  3. To save the project, on the main menu, click File -> Save All.
  4. To create the DLL, on the main menu, click Build -> Build ExoDLL.dll. At the end, you should receive Build: 1 succeeded, 0 failed, 0 skipped

Selecting the DLL

At this time, the DLL is ready. Probably the easiest way to use a DLL you have created is to create a program in the same folder and test it. To make it more fun, we will use our DLL in a separate program on another folder. the technique we will use is the same you would use if you want to create a program in the same folder the DLL project was created.

  1. To see the DLL, open Windows Explorer or My Computer.
  2. Locate the folder where the DLL project was created.
  3. Inside the folder, display the content of the Debug folder. Notice two files: one has the extension .dll and another has .lib.
  4. Click the ExoDLL.dll file to select it.
  5. Press and hold Ctrl. then click ExoDLL.lib file to select both
  6. Release Ctrl
     
  7. On the main menu of Windows Explorer or My Computer, click Edit -> Copy

Using the DLL in a Console Application

The DLL we have created can be used in a Console application or an MFC program. We will start with a console application.

  1. To start a new C++ program, on the main menu of MSVC .Net, click File -> New -> Project
  2. In the Project Types list, click Visual C++ Projects
  3. In the Templates list, click Win32 Project
  4. In the Project Name, type a name, such as ExoDLLTest
  5. Click OK
  6. In the Win32 Application Wizard, click Application Settings
  7. In the Application Type section, click Console Application
  8. In the Additional Options section, click the Empty Project check box and click Finish
  9. Go back to Windows Explorer or My Computer
  10. Locate the folder that has the current exercise. For me, that would be the ExoDLLTest folder
  11. Right-click in the folder and click Paste.
    The previously copied files should be pasted now in the new project folder
     
  12. To create a new source file, on the main menu of MSVC, click Project -> Add New Item...
  13. In the Add New Items dialog box, int the Templates section, click C++ File (.cpp)
  14. In the Name edit box, type a name such as Exercise and click Open
  15. To use the DLL, in the empty file, type the following:
     
    #include <iostream.h>
    
    extern "C" __declspec(dllimport)void BoxProperties(double L, double H,
                                        double W, double& A, double& V);
    
    int main()
    {
        double Length, Height, Width, Area, Volume;
    
        cout << "Enter the dimensions of the box\n";
        cout << "Length: ";
        cin >> Length;
        cout << "Height: ";
        cin >> Height;
        cout << "Width: ";
        cin >> Width;
    
        BoxProperties(Length, Height, Width, Area, Volume);
    
        cout << "\nProperties of the box";
        cout << "\nLength:  " << Length;
        cout << "\nHeight:  " << Height;
        cout << "\nWidth:   " << Width;
        cout << "\nArea:    " << Area;
        cout << "\nVolumne: " << Volume;
    
        cout << "\n\n";
    
        return 0;
    }
  16. To make the function of the DLL as part of the current project, on the main menu, Project -> Add Existing Item...
  17. In the Add Existing Item dialog box, change the Files Of Type combo box to All Files
  18. In the list of files, click ExoDLL.lib:
     
  19. Click Open
  20. To execute the program, press Ctrl + F5. Here is an example:
     
    Enter the dimensions of the box
    Length: 16.52
    Height: 12.74
    Width: 8.46
    
    Properties of the box
    Length:  16.52
    Height:  12.74
    Width:   8.46
    Area:    916.009
    Volumne: 1780.53
    
    Press any key to continue
  21. Close the DOS window and return to MSVC

Using the DLL in an MFC Application

We will proceed with the same ease to use our DLL in an MFC application.

  1. To create a new project, on the main menu, click File -> New -> Project...
  2. In the Project Types list, click Visual C++ Projects
  3. In the Templates list, click MFC Application
  4. In the Name edit box, type a name such as ExoDLLTest2
  5. Click OK
  6. In the MFC Application Wizard, click Application Type
  7. In the Application Type section, click Dialog Based and click Finish
  8. Open Windows explorer or My Computer. Locate the folder where the ExoDLL project was created and copy the ExoDLL.dll and ExoDLL.lib files
  9. Paste them in the ExoDLLTest2 folder
  10. Return to MSVC
  11. To make the function of the DLL as part of the current project, on the main menu, Project -> Add Existing Item...
  12. In the Add Existing Item dialog box, change the Files Of Type combo box to All Files
  13. In the list of files, click ExoDLL.lib and click Open
  14. Design the dialog box as follows:
     
    Control ID Caption Others
    Static Text   Length:  
    Edit Control IDC_LENGTH   Align Text: Right
    Static Text   Width:  
    Edit Control IDC_WIDTH   Align Text: Right
    Static Text   Height  
    Edit Control IDC_HEIGHT   Align Text: Right
    Button IDC_CALCULATE_BTN C&alculate Default Button: True
    Static Text   Area:  
    Edit Control IDC_AREA   Align Text: Right
    Static Text   Volume:  
    Edit Control IDC_VOLUME   Align Text: Right
    Button IDCANCEL Close  
  15. To prepare the dialog for coding, right-click each Edit Control and click Add Variable
  16. In the Category combo box, select Value and click the Name edit box. Create the control variable as follows:
     
    ID Type Name
    IDC_LENGTH CString m_Length
    IDC_WIDTH CString m_Width
    IDC_HEIGHT CString m_Height
    IDC_AREA CString m_Area
    IDC_VOLUME CString m_Volume
  17. In the dialog box, double-click the Calculate button and change the file as follows:
     
    // ExoMFCUsingDLLDlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "ExoMFCUsingDLL.h"
    #include "ExoMFCUsingDLLDlg.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    extern "C" __declspec(dllimport)void BoxProperties(double L, double H,
                                        double W, double& A, double& V);
    
    /////////////////////////////////////////////////////////////////////////////
  18. To make the library part of the current project, on the main menu, click Project -> Add To Project -> Files...
  19. On the Insert Files Into Project dialog box, change the Files Of Type combo box to Library Files (*.lib).
  20. In the list of files, click ExoDLL.lib and click OK.
  21. Implement the OnCalculate function as follows:
     
    void CExoMFCUsingDLLDlg::OnCalculate() 
    {
    	// TODO: Add your control notification handler code here
    	double Length, Height, Width, Area, Volume;
    
    	UpdateData(TRUE);
    
    	Length = atof(m_Length);
    	Height = atof(m_Height);
    	Width  = atof(m_Width);
    
    	BoxProperties(Length, Height, Width, Area, Volume);
    
    	m_Area.Format("%.2f", Area);
    	m_Volume.Format("%.2f", Volume);
    
    	UpdateData(FALSE);
    }
  22. To test your program, press Ctrl + F5
     
 
 

Home Copyright © 2002-2006 FunctionX FunctionX