ArticleDirectory
- 1. Create DLL
- 2. Test the created DLL
1. Create dll1. create a new Win32 Empty Project mathlib in VC; 2. Add the pre-compiled header file stdafx. h and define the Import and Export Control symbols:
1:// Stdafx. h
2: # pragma once
3: # define mathlib_export
3. Add the header file mathlib. H that contains the global variables, functions, and classes to be exported:1:// Mathlib. h
2: # pragma once
3:
4: # ifdef mathlib_export
5: # define mathlibapi _ declspec (dllexport)
6 :#Else
7: # define mathlibapi _ declspec (dllimport)
8: # endif
9:
10:// Macro
11: # define PI 3.14149
12:
13:// Global variable
14:ExternMathlibapiIntGlobalvariable;
15:
16:// Function
17: mathlibapiIntAdd (IntA,IntB );
18:
19:// Class
20:ClassMathlibapi math
21 :{
22:Public:
23:IntMultiply (IntA,IntB );
24 :};
4. Add the exported element implementation file mathlib. cpp. 1:// Mathlib. cpp
2: # include"Stdafx. h"
3: # include"Mathlib. h"
4:
5:IntGlobalvariable = 100;
6:
7:IntAdd (IntA,IntB)
8 :{
9:ReturnA + B;
10 :}
11:
12:IntMath: multiply (IntA,IntB)
13 :{
14:ReturnA * B;
15 :}
2. Test the created DLL
TestCode:
1: # include"Stdafx. h"
2: # include <iostream>
3:Using NamespaceSTD;
4:
5: # include"../Mathlib. h"
6: # pragma comment (Lib ,"../Debug/mathlib. Lib")
7:
8:Int_ Tmain (IntArgc, _ tchar * argv [])
9 :{
10: cout <"Pi ="<PI <Endl;
11:
12: cout <"Globalvariable ="<Globalvariable <Endl;
13:
14:IntA = 20, B = 30;
15: cout <"A ="<A <","<"B ="<B <Endl;
16: cout <"A + B ="<Add (a, B) <Endl;
17:
18: Math math;
19: cout <"A * B ="<Math. Multiply (a, B) <Endl;
20:
21:Return0;
22 :}