Creating and Using A Dynamic Link Library (C++)
Creating and Using A Dynamic Link Library (C++)
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
This step-by-step walkthrough shows how to create a dynamic link library (DLL) for use with a C++ app. Using a library is a great way to reuse code. Rather than re-implementing the same routines in every program that you create, you write them on
require the functionality. By putting code in the DLL, you save space in every app that references it, and you can update the DLL without recompiling all of the apps that use it. For more information about DLLs, see DLLs in Visual C++.
This walkthrough covers these tasks:
This walkthrough creates a DLL that can be called from apps that use C++ calling conventions. This requires both the DLL and the client app to be built by using the same compiler toolset, so that the internal naming conventions match. It's also possi
written in other languages and built using other compilers by using the C calling convention. For more information about specifying C linkage, see Exporting C++ Functions for Use in C-Language Executables. For information about how to create DLLs
Functions from Visual Basic Applications.
This simple walkthrough uses a combined solution that contains both the DLL and the client app, and uses implicit linking at load-time to import the DLLs functions. A more common situation involves third-party DLLs that are not built as part of your s
DLLs at run-time rather than at load-time. For more information about implicit linking and explicit linking, see Determining Which Linking Method to Use.
Prerequisites
This topic assumes that you understand the fundamentals of the C++ language and the basics of using the Visual Studio IDE. The Visual C++ components must be installed in Visual Studio to use this walkthrough.
C++
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
namespace MathLibrary
{
// This class is exported from the MathLibrary.dll
class Functions
{
public:
// Returns a + b
static MATHLIBRARY_API double Add(double a, double b);
// Returns a * b
static MATHLIBRARY_API double Multiply(double a, double b);
// Returns a + (a * b)
static MATHLIBRARY_API double AddMultiply(double a, double b);
};
}
This code declares a namespace, MathLibrary, that contains a class named Functions that contains member functions to perform some mathematical operations.
Notice the preprocessor statements at the top of the file. By default, the New Project template for a DLL adds PROJECTNAME_EXPORTS to the defined preprocessor symbols for the DLL project. In this example, MATHLIBRARY_EXPORTS
built. When the MATHLIBRARY_EXPORTS symbol is defined, the MATHLIBRARY_API symbol sets the __declspec(dllexport) modifier on the member function declarations in this code. This modifier tells the compiler and linker to expor
can be used by other applications. When MATHLIBRARY_EXPORTS is undefined—for example, when the header file is included by a client application—MATHLIBRARY_API defines the __declspec(dllimport) modifier on the member fun
import of the function in an application. For more information, see dllexport, dllimport.
Note
If you are building the DLL project on the command line, use the /D compiler option to define the MATHLIBRARY_EXPORTS symbol.
3. In the MathLibrary project in Solution Explorer, in the Source Files folder, open MathLibrary.cpp.
4. Implement the members of the Functions class in the source file. Replace the contents of the MathLibrary.cpp file with the following code:
C++
#include "stdafx.h"
#include "MathLibrary.h"
namespace MathLibrary
{
double Functions::Add(double a, double b)
{
return a + b;
}
5. To verify that everything is working so far, compile the dynamic link library by choosing Build, Build Solution on the menu bar. The output should look something like this:
Output
Note
If you are using an Express edition that does not display a Build menu, on the menu bar, choose Tools, Settings, Expert Settings to enable it, and then choose Build, Build Solution.
Note
If you are building a project on the command line, use the /D compiler option to define your project's PROJECTNAME_EXPORTS preprocessor symbol. Use the /LD compiler option to specify that the output file is to be a DLL. For more inform
Library). Use the /EHsc compiler option to enable C++ exception handling. For more information, see /EH (Exception Handling Model).
Congratulations, you've created a DLL using Visual C++! Next, you'll create a client app that uses the functions exported by the DLL.
Note
In older versions of Visual Studio, references are added to your project in a different way. Select the MathClient project in Solution Explorer, and then on the menu bar, choose Project, References. In the Property Pages dialog box, expand
Framework and References, and then choose the Add New Reference button. For more information about the References dialog box, see Adding references.
3. The Add Reference dialog box lists the libraries that you can reference. The Projects tab lists the projects in the current solution and any libraries that they contain. On the Projects tab, select the check box next to MathLibrary, and then choo
4. You need the definitions in the MathLibrary.h file to call the DLLs functions from your app. You could copy the header file into your client app project, but that might lead to changes in one copy that are not reflected in the other. To avoid this i
DLL, you can change the included directories path in your project to include the original header. To do this, open the Property Pages dialog box for the MathClient project. In the left pane, expand Configuration Properties, C/C++
the drop-down control next to the Additional Include Directories edit box, and then choose <Edit...>. Select the top pane of the Additional Include Directories dialog box to enable an edit control. In the edit control, specify the path to the
Because typing the complete path may be difficult, you can use the browse control (...) at the end of the edit box to bring up a Select Directory dialog box. In the dialog, navigate up one folder level to the MathLibraryAndClient
the Select Folder button. Once you've entered the path to the header file in the Additional Include Directories dialog box, choose the OK button to go back to the Property Pages dialog box, and then choose the OK button to save your cha
5. You can now include the MathLibrary.h file and use the Functions class in your client application. Replace the contents of MathClient.cpp by using the following code:
C++
#include "stdafx.h"
#include <iostream>
#include "MathLibrary.h"
int main()
{
double a = 7.4;
int b = 99;
return 0;
}
}
6. Build the application by choosing Build, Build Solution on the menu bar. The Output window in Visual Studio might contain something like this:
Output
Congratulations, you've created an application that calls functions in a DLL. Next, you'll run your application to see what it does.
Output
a + b = 106.4
a * b = 732.6
a + (a * b) = 740
Press any key to continue . . .
See Also
Visual C++ Guided Tour
DLLs in Visual C++
Deploying Desktop Applications
Walkthrough: Deploying Your Program (C++)
Calling DLL Functions from Visual Basic Applications
Dev centers
Windows
Office
Visual Studio
Microsoft Azure
More...
Learning resources
Micro so ft Virtu al Academy
Ch an n el 9
MSDN Magazin e
Community
Fo ru ms
B lo gs
Co dep lex
Support
Self su p p o rt
Programs
B izSp ark (fo r startu p s)
Micro so ft Imagin e (fo r stu den ts)
© 2017 Micro so ft