Environment: Visual C + + 2003, Windows
Source: DLL Project's source code and test project
I am learning DLLs, not to have any farsighted opinion of it; This article is just coding to let you see and wonder how the code works. In this article, I assume you know how to use your compiler features, such as setting directory paths, and so on.
To establish the project, select the Win32 console project (WIN32 Console application) and select the DLL and empty project options on the Application Settings tab (the Advanced tab). DLLs may not be as difficult as you think. First write your header (header file), called DLLTutorial.h. This file is just like any other header file, which is a prototype of some functions.
#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C"
{
DECLDIR int Add( int a, int b );
DECLDIR void Function( void );
}
#endif
The previous two lines indicate that the compiler contains only this file once. extern "C" tells the compiler that this section can be used in C + +.
In VC + + Here are two ways to export functions:
1, using __declspec, a Microsoft-defined keyword.
2, create a module definition file (module-definition file that is. DEF). The first method is slightly simpler than the second method, but both work well.
__declspec (dllexport) exports the function symbol to a storage class in your DLL. I define DECLDIR to run this function when the following line is defined.
#define DLL_EXPORT
Also import functions if the following line
#define DLL_EXPORT
is not present in the source file. In this case, you will export the function add (int a, int b) and functions ().
Now you need to write a source file that will be called DLLTutorial.cpp.
#include <iostream>
#include "DLL_Tutorial.h"
#define DLL_EXPORT
extern "C"
{
DECLDIR int Add( int a, int b )
{
return( a + b );
}
DECLDIR void Function( void )
{
std::cout << "DLL Called!" << std::endl;
}
}