Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C++
Article

Step by Step: Calling C++ DLLs from VC++ and VB - Part 1

Rate me:
Please Sign up or sign in to vote.
4.90/5 (139 votes)
29 Feb 2004CPOL6 min read 921.9K   8.9K   334   82
This series of articles is a step-by-step guide to constructing C++ DLLs that include C++ functions and C++ classes, and then calling the DLL functions and classes from VC++ and VB programs.

Introduction

This series of articles discusses four common situations when working with DLLs:

Part 1Calling a DLL C++ function from a VC++ application
Calling a DLL C++ class from a VC++ application
Part 2Calling a DLL C++ function from a VB application
Part 3Calling a DLL C++ class from a VB application
Part 4Loading a C++ DLL dynamically from a VC++ application

Calling a DLL C++ function or class from a VC++ application

Visual Studio 6 makes it very easy to create C++ DLLs that contain functions or C++ classes.

Step 1

Open Visual Studio and go to File | New:

Image 1

Select Win32 Dynamic Link Library, enter a project name, and hit OK.

Image 2

Select A DLL that exports some symbols and hit Finish. You will see the following files in the File View:

Image 3

Step 2

Inside Test.cpp, you will see this code:

// Test.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "Test.h"

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
    }
    return TRUE;
}


// This is an example of an exported variable
TEST_API int nTest=0;

// This is an example of an exported function.
TEST_API int fnTest(void)
{
    return 42;
}

// This is the constructor of a class that has been exported.
// see Test.h for the class definition
CTest::CTest()
{ 
    return; 
}

Test.cpp contains code for fnTest and CTest::CTest. If you compiled Test.dll at this point, you would have a DLL that could be called directly by other VC++ applications. The key mechanism that allows other VC++ applications to call Test.dll is contained in Test.h:

// The following ifdef block is the standard way of creating macros
// which make exporting from a DLL simpler. All files within this DLL
// are compiled with the TEST_EXPORTS symbol defined on the command line.
// This symbol should not be defined on any project that uses this DLL.
// This way any other project whose source files include this file see 
// TEST_API functions as being imported from a DLL, whereas this DLL
// sees symbols defined with this macro as being exported.
#ifdef TEST_EXPORTS
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif

// This class is exported from the Test.dll
class TEST_API CTest 
{
public:
    CTest(void);
    // TODO: add your methods here.
};

extern TEST_API int nTest;

TEST_API int fnTest(void);

What is going on here? What does #ifdef TEST_EXPORTS mean, and where is TEST_EXPORTS defined?

What is happening is that, depending on whether TEST_EXPORTS is defined, TEST_API will be defined to export or import the DLL symbols. The export process results in the DLL's symbols being defined in Test.lib, which can be linked to any VC++ application that wants to access the DLL. When compiling Test.dll, I want to export, which means I want TEST_EXPORTS to be defined.

Step 3

Where is TEST_EXPORTS defined? This is one of the things I don't like about the DLL wizard - it puts the definition of TEST_EXPORTS on the command line. Go to Project | Settings | C/C++ | General, and you will see the Project Options:

Image 4

While this method certainly works, it is obscure, and may cause maintenance problems later on. I prefer to make explicit the definition of TEST_EXPORTS. I do this by deleting /D "TEST_EXPORTS" from the Project Options, and then changing Test.cpp to define TEST_EXPORTS:

// Test.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#define TEST_EXPORTS                     // <===  ADD THIS LINE
#include "Test.h"

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved)
{
.
.
.

Notice that the #define TEST_EXPORTS is before the line #include "Test.h", so that the definition will be seen in the include file. Now Test.dll compiles just as before, and you will have a DLL that can be called from other VC++ applications.

Step 4

How can I call the functions in this DLL? To illustrate this, I create a demo app using Visual Studio. Select MFC AppWizard (exe), enter a project name, and click OK. Select Dialog based, then click Finish. Open the file XXXDlg.cpp, where XXX is the project name. Go to the OnInitDialog() method, and enter the following code:

    .
    .
    .
    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // code to test Test.dll function:
    int n = fnTest();                    // <=== ADD THIS LINE

    // code to test the CTest class:
    CTest test;                          // <=== ADD THIS LINE
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}

Step 5

This code won't compile until I include the Test.h file from the DLL:

// TestExeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestExe.h"
#include "TestExeDlg.h"
#include "Test.h"                        // <=== ADD THIS LINE
.
.
.

Step 6

If you are doing a quickie demo, you will probably be tempted just to copy the DLL's Test.h into your exe project directory, so the compiler will be able to find it. For larger projects, this is not a good idea, because there is too much risk that you will update the DLL file, but forget to copy it into your exe directory. There is a simple way to solve this: go to Project | Settings | C/C++ | Settings | Preprocessor, and add to the Additional include directories field:

Image 5

Note: This assumes that the DLL project and the EXE project have the same parent directory.

Now when I compile, I get ... linker errors!

Deleting intermediate files and output files for project 'TestExe - Win32 Debug'.
--------------------Configuration: TestExe - Win32 Debug--------------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
TestExe.cpp
TestExeDlg.cpp
Generating Code...
Linking...
TestExeDlg.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) 
public: __thiscall CTest::CTest(void)" (__imp_??0CTest@@QAE@XZ)
TestExeDlg.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) 
int __cdecl fnTest(void)" (__imp_?fnTest@@YAHXZ)
Debug/TestExe.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

TestExe.exe - 3 error(s), 0 warning(s)

Step 7

Although I told the compiler about the DLL symbols, I also must tell the linker. Go to Project | Settings | Link and add the DLL's LIB file to the Object/library modules field:

Image 6

Step 8

Now I get a clean compile. Before running the application, there is one more thing to do: copy Test.dll to the EXE's directory.

Step 9

I can then put a breakpoint in the OnInitDialog() method, and hit Go (F5):

Image 7

and I see that fnTest has returned 42 as expected. The CTest class can be tested in a similar way.

Key Concepts

  • The Visual Studio project wizard provides a good starting point for creating a VC++ DLL.
  • Functions, classes, and variables can be exported from the DLL.
  • Using #define preprocessor definitions, one include file can be used by both the DLL and the application.
  • The DLL exports its symbols, and the application imports the DLL symbols. When compiling the application, the compiler sees the DLL symbols via an include file (Test.h). When linking the application, the linker sees the DLL symbols via the import library (Test.lib).
  • The DLL must be in the same directory as the EXE when running the application. In early versions of Windows, it was acceptable practice to put application DLLs in the Windows or System directories, but this is now recognized to cause problems, and should not be done.

Comments

In practice, I never use the approach described in Step 7. For large projects, the number of DLLs and LIB files quickly becomes unmanageable if handled in this way. What you want to do is set up project lib and bin directories, where you can keep copies of all current LIB, DLL, and EXE files. How do you tell the linker where to find these LIB files? There are two ways to do this in Visual Studio:

  1. Go to Tools | Options | Directories and set Show directories for to "Library files". Now enter the path to where your project's LIB files are.

    Image 8

  2. Alternatively, go to Project | Settings | Link, select the Input category, and enter the Additional library path for your project's LIB files.

    Image 9

Which method is better? That depends on the situation in your development environment. Method 1 means all projects on a machine will share the same set of directory paths, but each developer's Visual Studio must be set up with those paths.

Method 2 means that the paths can be customized for each project, and will be stored inside the project, so any developer can check out the project and begin development - provided the same paths exist on the developer's machine.

It is common to establish standard drive+directory mappings, to eliminate endlessly tweaking the directory paths on each developer's machine.

I still haven't discussed how to specify what LIB files to use. Here is my trick: In the DLL's Test.h, add two lines. The DLL1.h file now looks like:

#ifdef TEST_EXPORTS
    #define TEST_API __declspec(dllexport)
#else
    #define TEST_API __declspec(dllimport)
    #pragma message("automatic link to Test.lib")   // <== add this line
    #pragma comment(lib, "Test.lib")                // <== add this line
#endif

// This class is exported from the Test.dll
class TEST_API CTest 
{
public:
    CTest(void);
};

extern TEST_API int nTest;

TEST_API int fnTest(void);

This ensures that when a project includes the DLL's header file, it will also automatically link to the DLL's LIB file. The developer is notified of this by the #pragma message, which appears in the output window of Visual Studio.

Demo

The EXE1.exe demo tests the GetCpuSpeed() function and CDLL1::GetCpuSpeed() class method in DLL1.dll:

Image 10

Revision History

Version 1.0 - 2004 February 29

  • Initial public release

Usage

This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
QuestionPossibly update this? Pin
ReturnVoid5-Apr-16 22:15
ReturnVoid5-Apr-16 22:15 
I've found this info useful, however it is old in programming terms. Any possibility of updating this tutorial & project? ie to include VS2010/2013+ ? As there are some very subtle differences.
QuestionHELP: DLL for template class Pin
Member 191289413-Nov-14 2:51
Member 191289413-Nov-14 2:51 
QuestionDLL(s) article Pin
OLIRAQ12-Jul-14 20:16
OLIRAQ12-Jul-14 20:16 
GeneralMy vote of 5 Pin
gndnet23-Sep-12 3:24
gndnet23-Sep-12 3:24 
GeneralMy vote of 5 Pin
gndnet14-Aug-12 0:19
gndnet14-Aug-12 0:19 
GeneralMy vote of 5 Pin
User 5924112-Jul-12 20:32
User 5924112-Jul-12 20:32 
GeneralMy vote of 5 Pin
Sergey Alexandrovich Kryukov26-May-11 9:38
mvaSergey Alexandrovich Kryukov26-May-11 9:38 
GeneralMatrix between VB6 and C++ Pin
francomulato1-Apr-10 6:51
francomulato1-Apr-10 6:51 
QuestionHow about ActiveX DLL? Pin
transoft4-Oct-09 3:50
transoft4-Oct-09 3:50 
GeneralDLL that returns a string to Visual Basic 6 program Pin
Captain 242-Oct-09 2:10
Captain 242-Oct-09 2:10 
GeneralRe: DLL that returns a string to Visual Basic 6 program Pin
Hans Dietrich2-Oct-09 22:54
mentorHans Dietrich2-Oct-09 22:54 
QuestionCan we use DLLClassObject ? Pin
JothiMurugeswaran20-Nov-07 23:20
JothiMurugeswaran20-Nov-07 23:20 
GeneralCreating C++ DLL to be used by Java Script Pin
El Tenia13-Sep-07 18:15
El Tenia13-Sep-07 18:15 
GeneralRe: Creating C++ DLL to be used by Java Script Pin
El Tenia13-Sep-07 18:27
El Tenia13-Sep-07 18:27 
GeneralRe: Creating C++ DLL to be used by Java Script Pin
onlinewan19-Sep-07 14:40
onlinewan19-Sep-07 14:40 
GeneralRe: Creating C++ DLL to be used by Java Script Pin
flyingdonkey19-Sep-07 22:25
flyingdonkey19-Sep-07 22:25 
GeneralRe: Creating C++ DLL to be used by Java Script Pin
Ash_VCPP17-Mar-09 22:23
Ash_VCPP17-Mar-09 22:23 
QuestionHelp me~~ Pin
raid_zh27-Mar-07 22:23
raid_zh27-Mar-07 22:23 
AnswerRe: Help me~~ Pin
Y_R14-Jul-07 8:54
Y_R14-Jul-07 8:54 
GeneralLinker Error Pin
jaganit2-Mar-07 1:49
jaganit2-Mar-07 1:49 
GeneralUnicode and Win32 DLL Pin
zou chunzhong14-Dec-06 22:20
zou chunzhong14-Dec-06 22:20 
GeneralDLL for Matlab Pin
maritkap5-Dec-06 22:40
maritkap5-Dec-06 22:40 
GeneralLinker Error Pin
mshubhu29-Oct-06 23:16
mshubhu29-Oct-06 23:16 
General.NET dll's Pin
zooley28-Oct-06 20:21
zooley28-Oct-06 20:21 
GeneralRe: .NET dll's Pin
Hans Dietrich29-Oct-06 22:11
mentorHans Dietrich29-Oct-06 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.