Create a new DLL project :
New-Project-VC + +-WIN32-WIN32 console application, project name assumed to be MathFuncsDll
The wizard selects the DLL, cancels the precompiled header
Complete.
At this time there will be Dllmain.cpp, MathFuncsDll.cpp two CPP files and generated stdafx.h, Targetver.h Stdafx.cpp, the following three files are actually useless, you can delete, and then put the dllmain.cpp in the # Include "StdAfx.h" to # include <windows.h>
Add a header file to the project MathFuncsDll.h used to define our function, now the project file
There are two ways to export functions in a DLL, one is to use the __declspec (dllexport) statement to declare a function , so that you do not need a DEF file to export functions, such as
extern " C " Double ADD1 (doubledouble b);
The corresponding function definition
Double ADD1 (doubledouble b) { return a + b; }
Here the extern "C" is to generate in the DLL function name does not change, if not the sentence, Add1 in the DLL's export function may be called [e-mail protected]@, etc., inconvenient dynamic call (LoadLibrary, GetProcAddress).
Through Denpency Walker can see the DLL's export function and its name, the name shown here is the GetProcAddress () function needs the name, if the name is not the function will not get
Another approach is to use DEF files , which get a better view of the exported function name, suitable for dynamic invocation. At this point the definition of the function can be written in a relatively simple
Double Divide2 (doubledouble b); Double Divide2 (doubledouble b) { if0) Throw invalid_argument ("b cannot be zero! " ); return A/ b;}
Insert the DEF file into the project and write it in the following code
LIBRARY Mathfuncsdllexports Divide2 @1
The first row is the Library keyword, followed by the repository name
The second line is the EXPORTS keyword
Further down is the name of the function to be exported and its ordinal number.
Attached code:
//MathFuncsDll.cpp: Defines an export function for a DLL application. //#include"MathFuncsDll.h"#include<stdexcept>using namespacestd;namespaceMathfuncs {DoubleMymathfuncs::add (DoubleADoubleb) {returnA +b; } DoubleMymathfuncs::subtract (DoubleADoubleb) {returnAb; } DoubleMymathfuncs::multiply (DoubleADoubleb) {returnAb; } DoubleMymathfuncs::D ivide (DoubleADoubleb) {if(b = =0) ThrowInvalid_argument ("b cannot be zero!"); returnAb; }}mathfuncsdll_apiDoubleADD1 (DoubleADoubleb) {returnA +b; }mathfuncsdll_apiDoubleSubtract1 (DoubleADoubleb) {returnAb; }mathfuncsdll_apiDoubleMultiply1 (DoubleADoubleb) {returnAb; }mathfuncsdll_apiDoubleDivide1 (DoubleADoubleb) {if(b = =0) ThrowInvalid_argument ("b cannot be zero!"); returnAb;}DoubleDivide2 (DoubleADoubleb) {if(b = =0) ThrowInvalid_argument ("b cannot be zero!"); returnAb;}
Header file:
#pragmaOnce#ifdef mathfuncsdll_exports#defineMathfuncsdll_api __declspec (dllexport)#else#defineMathfuncsdll_api __declspec (dllimport)#endifnamespaceMathfuncs {classMymathfuncs { Public: StaticMathfuncsdll_apiDoubleADD (DoubleADoubleb); StaticMathfuncsdll_apiDoubleSubtract (DoubleADoubleb); StaticMathfuncsdll_apiDoubleMultiply (DoubleADoubleb); StaticMathfuncsdll_apiDoubleDivide (DoubleADoubleb); };}extern "C"Mathfuncsdll_apiDoubleADD1 (DoubleADoubleb);extern "C"Mathfuncsdll_apiDoubleSubtract1 (DoubleADoubleb);extern "C"Mathfuncsdll_apiDoubleMultiply1 (DoubleADoubleb);extern "C"Mathfuncsdll_apiDoubleDivide1 (DoubleADoubleb);DoubleDivide2 (DoubleADoubleb);
In addition to the normal functions, there are static functions for the classes in the namespace. However, this function will be difficult to invoke through GetProcAddress (), which is easier to invoke by linking Lib files, such as
" <<<< Endl;
Creation of DLLs in VS and function export (refer to MSDN)