Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Basic C++ Win32 DLL

0.00/5 (No votes)
26 Sep 2013 1  
Basic steps of creating and consuming a C++ Win32 DLL.

Introduction

In this article, we will create a small and simple Win32 C++ DLL project using an Empty Project template from Visual Studio 2010 and then create a separate Win32 Console Application to consume the DLL functionalities.

There are many uses of DLL but this article only focuses on how to create and use a C++ Win32 DLL as well as giving you a skeleton on the structure of a C++ Win32 DLL. A C++ Win32 DLL produces binary code. Note that there is also another kind of DLL which is a CLR DLL. CLR DLL does not produce binary code but it generates an Intermediate Language code or IL code which is not covered in this article. CLR DLLs are called managed code while C++ Win32 DLLs are called unmanaged code. CLR codes are those codes made from .NET Framework languages like C#, VB.NET, or with VC++ .NET.

Background

You can simply think of a DLL or Dynamic Link Library as a collection of codes, data, or resources that is packed together into one file. DLL is a great way to distribute and reuse code. For example you have two applications that use arithmetic operations like Addition, Subtraction, Multiplication, and Division, instead of coding the same operations in each application, you can just put these operations inside a DLL (e.g., Math.dll) and reference the DLL from the two applications. In this manner, you only code the math operations once, while your two applications or even your future applications can use the same Math.dll for arithmetic operations.

More of that, it would be easier to understand if we will just see the working code, right? So let's dive into the code project!

Using the code

First, let's create a C++ Win32 DLL using Visual Studio 2010. (It is recommended to use VS 2010 since this was the actual IDE used to build this project. Other version may be used but with some slight differences.)

Let's open Visual Studio 2010 and select "File > New Project > Visual C++ > Win32 > Win32 Project" and name the new project "MyWin32CppDLL" and then Press OK. A Win32 Application Wizard window will popup - click Next button and then select the Application type: "DLL" and tick Additional options: "Empty project" and finally click the Finish button.

Then let's add four files into our project (MyWin32ClassOne.h, MyWin32ClassOne.cpp, MyWin32ClassTwo.h, and MyWin32ClassTwo.cpp). I just wanted to add two classes into our DLL to illustrate that we can add classes as many as we like into a DLL.

Place the below code to their respective files.

MyWin32ClassOne.h

namespace MyWin32DLL
{
    class MyWin32ClassOne
    {
    private:
        int varone;
    public:
        __declspec(dllexport) MyWin32ClassOne();//constructor
        __declspec(dllexport) void Setvar(int val);
        __declspec(dllexport) int Getvar();
    };
} 

MyWin32ClassOne.cpp

#include "MyWin32ClassOne.h"

namespace MyWin32DLL
{
    MyWin32ClassOne::MyWin32ClassOne()
    {
        varone = 123;
    }
 
    void MyWin32ClassOne::Setvar(int val)
    {
        varone = val;
    }
 
    int MyWin32ClassOne::Getvar()
    {
        return varone;
    }
} 

MyWin32ClassTwo.h

namespace MyWin32DLL
{
    class MyWin32ClassTwo
    {
    private:
        int vartwo;
    public:
        __declspec(dllexport) MyWin32ClassTwo();//constructor
        __declspec(dllexport) void Setvar(int val);
        __declspec(dllexport) int Getvar();
    };
} 

MyWin32ClassTwo.cpp

#include "MyWin32ClassTwo.h"

namespace MyWin32DLL
{
    MyWin32ClassTwo::MyWin32ClassTwo()
    {
        vartwo = 345;
    }

    void MyWin32ClassTwo::Setvar(int val)
    {
        vartwo = val;
    }
 
    int MyWin32ClassTwo::Getvar()
    {
        return vartwo;
    }
}  

Build the project and look for the output DLL in the Debug/Release folder. We have now created our C++ Win32 DLL!

What's the purpose of a DLL if there's no one that uses it, right? So let's create a simple Win32 Console Application that will use our DLL's functionalities.

In Visual Studio, add a new Win32 Console Project as an Empty Project and name it "MyWin32CppApp" and then add a source file named "Main.cpp" just like below.

Add below code to Main.cpp:

Main.cpp

#include<iostream>//for cin, cout and endl
using namespace std;
 
#include "MyWin32ClassOne.h" 
#include "MyWin32ClassTwo.h" 
using namespace MyWin32CppDLL;
 
int main()
{
    cout<<"Hello C++ Win32 DLL"<<endl;
 
    MyWin32ClassOne sc1;
    cout<<"default value of variable from dll : "<<sc1.Getvar()<<endl;
    sc1.Setvar(101);
    cout<<"value of variable from dll : "<<sc1.Getvar()<<endl;
 
    MyWin32ClassTwo sc2;
    cout<<"default value of variable from dll : "<<sc2.Getvar()<<endl;
    sc2.Setvar(200);
    cout<<"value of variable from dll : "<<sc2.Getvar()<<endl;
 
    cin.get();//pause console to see the message

    return 0;
}

Before we can build our application correctly, we need to add a reference to our DLL. Right click the Application project and select "Properties" to open its property page.

First, navigate to "C/C++ > General > Additional Include Directories" then assign the full path where the header files of the DLL are located. This will tell the compiler to locate MyWin32ClassOne.h and MyWin32ClassTwo.h in this location. Click OK.

Second, right click Application project and select "References...". Click "Add New References" and select the DLL project we previously created. Click OK. Finally, build the Solution as Release. Navigate to "Release" folder and launch MyWin32CppApp.exe.

Points of Interest

When we add the path to the header files in our application project, we are just telling the compiler to locate the header files we are using from that location. But when we add reference to our DLL project from our application project, we are basically telling the Linker to link the header files of the actual implementation of our Win32 DLL. 

Advanced points to ponder: 

  •  C++ DLL built from different compilers may not be consumed by a C++ Application/other C++ DLL directly because C++ compilers have different "name mangling" specifications. *This is why we need to compile the source of a C++ DLL project along with our C++ application to ensure they are built using the same compiler.
  • For a C++ DLL to be imported in a C application, global functions.
  • In .NET C#, you can only "DllImport" or "PInvoke" globally exported functions from a C++ DLL.

Hope this helps.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here