0% found this document useful (0 votes)
112 views4 pages

Building A DLL With Visual C++

This document provides instructions for building a dynamic link library (DLL) using Microsoft Visual C++. It outlines the key steps: 1. Create a DLL project and add source code with exported functions. 2. Specify exported functions using __declspec(dllexport) tags or a .def file. 3. Set the calling convention to __cdecl or __stdcall. 4. Build the project to generate the DLL file. The document includes examples of exporting functions from a DLL and accessing them from LabVIEW.

Uploaded by

magicecstatic
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
112 views4 pages

Building A DLL With Visual C++

This document provides instructions for building a dynamic link library (DLL) using Microsoft Visual C++. It outlines the key steps: 1. Create a DLL project and add source code with exported functions. 2. Specify exported functions using __declspec(dllexport) tags or a .def file. 3. Set the calling convention to __cdecl or __stdcall. 4. Build the project to generate the DLL file. The document includes examples of exporting functions from a DLL and accessing them from LabVIEW.

Uploaded by

magicecstatic
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

1/4 www.ni.

com
1.
2.
3.
4.
5.
BuiIding a DLL with VisuaI C++
Publish Date: Mar 16, 2012 | 189 Ratings | out of 5 4.10
Overview
Microsoft's Visual C++ (MSVC) integrated development environment (DE) can be overwhelming if the programmer has never used it. This document is designed to aid those wanting to compile a
DLL for use with LabVEW.
: This document applies to MSVC 2010. Note
TabIe of Contents
Step 1: Creating a DLL Project
Step 2: Editing the Source File
Step 3: Exporting Symbols
Step 4: Specifying the Calling Convention
Step 5: Building the DLL
1. Step 1: Creating a DLL Project
Select to open the New Project dialog box. From the list, select , name your project and click . FiIeNew Project VisuaI C++ TempIates Win32 Project OK
n the next dialog box, you may see the current project settings to be Windows Application. Click to change the Application Type to . Next DLL
2/4 www.ni.com
MSVC creates a DLL project with one source ( ) file, which has the same name as the project. t also generates a file. The file is necessary, but you do not generally .cpp stdafx.cpp stdafx.cpp
need to edit it.
2. Step 2: Editing the Source FiIe
Every DLL file must have a function, which is the entry point for the library. Unless you must do a specific initialization of the library, the default that MSVC created is sufficient. DllMain DllMain
Notice that this function does nothing.
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
return TRUE;
}
f a library initialization is required, you might need a more complete : DllMain
BOOL WINAPI DllMain(
HINSTANCEhinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpReserved ) // reserved
{
// Perform actions based on the reason for calling.
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;

case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;

case DLL_PROCESS_DETACH:
// Perform any necessary cleanup.
break;
}
return TRUE;
}
Once the function is complete, write the routines that you intend to access from the DLL. DllMain
//Function declarations
; int GetSphereSAandVol(double radius, double* sa, double* vol)
double GetSA(double radius);
double GetVol(double radius);
...
int GetSphereSAandVol(double radius, double* sa, double* vol)
//Calculate the surface area and volume of a sphere with given radius
{
if(radius < 0)
return false; //return false (0) if radius is negative
*sa = GetSA(radius);
*vol = GetVol(radius);
3/4 www.ni.com
*vol = GetVol(radius);
return true;
}
double GetSA(double radius)
{
return 4 * M_PI * radius * radius;
}
double GetVol(double radius)
{
return 4.0/3.0 * M_PI * pow(radius, 3.0);
}
For the DLL to compile correctly, you must declare the function (i.e. power, is equivalent to x^y) and the constant M_P (i.e. 3.14159). pow pow(x,y)
Do this by inserting two lines of code below at the top of the file. The code should look as follows: #include "stdafx.h" .cpp
#include "stdafx.h"
#include "math.h" //library that defines the pow function
#define M_PI 3.14159 //declare our M_PI constant
At this point, you can compile and link the DLL. However, if you do so, the DLL will not export any functions, and thus, will not really be useful.
3. Step 3: Exporting SymboIs
To access the functions within the DLL, it is necessary to tell the compiler to export the desired symbols. However, you first must address the issue of C++ name decoration. MSVC compiles your
source as C++ if it has a or extension. f the source file has a extension, then MSVC compiles it as C. f you compile your file as C++, then the function names are normally decorated .cpp .cxx .c
in the output code. This might be problematic because the function name has extra characters added to it. To avoid this problem, declare the function as 'extern "C"' in the function declaration, as
follows:
; extern "C" int GetSphereSAandVol(double radius, double* sa, double* vol)
This prevents the compiler from decorating the name with C++ decorations.
Warning: Without C++ decoration, polymorphic functions are not possible.
When you finish with the C++ decorations, you can actually export the functions. There are two methods to inform the linker which functions to export. The first, and most simple, is to use the
tag in the function prototype for any function you want to export. To do this, add the tag to the declaration and definition, as follows: __declspec(dllexport)
extern "C" __declspec(dllexport) int GetSphereSAandVol(double radius, double* sa, double* vol);
...
__declspec(dllexport) int GetSphereSAandVol(double radius, double* sa, double* vol)
{
...
}
The second method is to use a file to explicitly declare which functions to export. The file is a text file that contains information the linker uses to decide what to export. t has the following .def .def
format:
LIBRARY <Name to use inside DLL>
DESCRIPTION "<Description>"
EXPORTS
<First export> @1
<Second export> @2
<Third export> @3
...
For the example DLL, the file will look like this: .def
LIBRARY EasyDLL
DESCRIPTION "Does some sphere stuff."
EXPORTS
GetSphereSAandVol @1
f you have properly created your DLL project, then the linker automatically looks for a file of the same name as the project in the project directory. To change this option, select .def
. n the folder, click the property page and modify the property to . ProjectProperties Linker Input ModuIe Definition FiIe /DEF: <fiIename>.def
4/4 www.ni.com
See Also:
Microsoft's .DEF file method documentation
4. Step 4: Specifying the CaIIing Convention
The last thing that you might need to do before compiling the DLL is to specify the calling convention for the functions that you want to export. Usually, there are two choices: C calling convention or
standard calling conventions, also called Pascal and WNAP. Most DLL functions use standard calling conventions, but LabVEW can call either.
To specify C calling conventions, you do not need to do anything. This is the default unless you specify otherwise in . f you want to explicitly declare the ProjectPropertiesC/C++Advanced
function as a C call, use the keyword in the function declaration and definition: __cdecl
extern "C" __declspec(dllexport) int __cdecl GetSphereSAandVol(double radius, double* sa, double* vol);
...
__declspec(dllexport) int __cdecl GetSphereSAandVol(doublt radius, double* sa, double* vol)
{
...
}
To specify standard calling conventions, place the keyword in the function declaration and definition: __stdcall
extern "C" int __stdcall GetSphereSAandVol(double radius, double* sa, double* vol);
...
int __stdcall GetSphereSAandVol(doublt radius, double* sa, double* vol)
{
...
}
When using standard calling conventions, the function name is decorated in the DLL. You can avoid this by using the file method of exporting functions, rather than the .def
method. Therefore, National nstruments recommends that you use the file method to export stdcall functions. __declspec(dllexport) .def
5. Step 5: BuiIding the DLL
Once you write the code, declare what functions to export, and set the calling conventions, you are ready to build your DLL. Select to compile and link your DLL. You BuiIdBuiId <Your project>
are now ready to use or debug your DLL from LabVEW. The attached file contains the Visual C++ workspace used to create this DLL and a LabVEW V that accesses the DLL. EasyDLL.zip

You might also like