How to Create a DLL (Dynamic Link Library) in Visual C++

  We all come across dynamic link libraries (DLLs) aka shared libraries while developing Windows programs. These libraries contain definitions of various functions whose declarations have been made in header files. This article shows you how to create a DLL.

What is a DLL?

As the name implies, dynamic link libraries are accessed by a program at run-time rather than at the time of compilation/ linking. Therefore, they do not have to be linked with a program while creating an executable of that program. They are linked dynamically to the program(s) which need to use them at run-time.

How to Create a DLL?

This tutorial shows you how to create a dll i.e. dynamic link library in Visual C++ using Visual Studio IDE.

1. Create a Win32 Console Application project in Visual C++.

Create a Win32 Console Application in Visual C++

2. Click Next.

Click Next

3. Select the radio button labelled DLL and click Finish.

Select the radio button labelled DLL and click finish

4. The project is created and it already contains two .cpp files named dllmain.cpp (present in every DLL) and mydll.cpp (source file whose name varies, depending upon the name of the project).

dllmain.cpp
mydll.cpp

5. Add a header file.

Add a header file

6. Add the following statements in the header file :


#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif

Below these statements, add the declarations of the functions you want to define in your DLL.
For example,


namespace MYDLL
{
  class MYDLL
  {
    public:
      static MYDLL_API double Add(double a, double b);
      static MYDLL_API double Subtract(double a, double b);
      static MYDLL_API double Multiply(double a, double b);
      static MYDLL_API double Divide(double a, double b);
  };
}

 

Add these statements to the header file

7. In the source file (in this case, mydll.cpp), add the following statements below #include “stdafx.h” :


#include "mydll.h"
#include <stdexcept>

using namespace std;


After this, add the definitions of the functions declared in the header file.

Add this code to mydll.cpp

 8. Build the project.

Build the project

 9. The DLL has been created and is present in the Debug folder of the current project. Copy the mydll.lib file to “C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\lib” (depending upon the version of Visual Studio you have).

Please Note that the Program Files (x86) folder exists only on 64-bit versions of Windows. On 32-bit versions of Windows, you need to use the Program Files folder.

These files are mydll.dll and mydll.lib
Move these two files to the lib folder of Visual C++ (may vary depending on your version of Visual C++)


Testing the DLL created above


1. Create a new Win32 Console Application project in Visual C++.

Create a new Win32 Console Application in Visual C++

2. Click Next.

Click Next

 3. Uncheck the check box labelled Precompiled header and click Finish.

Uncheck the check box labelled "Precompile header" and click "finish"

4.  Add the following code to the source file of this project :

Add the following code to the source file of this project

  
5.  Add “mydll.h” and “mydll.dll” to the folder named mdllcheck.

All "mydll.dll" and "mydll.h" to this project's folder

6. Build this project.

Build this project

7. The output of this program is displayed below.

This is the output of this project



You can download the above DLL project here.

Did you like this article? Let us know in your comments below.

How to Create a Static Library in Visual C++
Static Libraries vs Dynamic Libraries : Advantages and Disadvantages 

(Visited 660 times, 1 visits today)

4 comments

  • This is really interesting, You're very skilled blogger.
    I have joined your rss feed and look forward to seeking more
    of your wonderful post. Also, I've shared your site
    in my social networks!

  • Thanks for your feedback. We really appreciate it…

  • The sole purpose of using DLLs is to share code among different programs. Using DLLs allows you to write a certain piece of code once, and use it with many other programs. You don't need to use DLLs if you have no intention to create a shareable library.

  • Is it necessary to use DLLs? Can't I just put all my code in .cpp files and .h files?

Leave a Reply

Your email address will not be published. Required fields are marked *