Create a dynamic library dll project first
Add dlltest. cpp dlltest. def dlltest. h to the Project
Dlltest. h
[Cpp]
// Dlltest. h
Extern _ declspec (dllexport) int FuncTest ();
Dlltest. cpp
[Cpp]
// Dlltest. cpp
_ Declspec (dllexport) int FuncTest (int)
{
If (a = 1)
{
Return 100;
}
}
Dlltest. def
[Cpp]
LIBRARY "testmydll"
EXPORTS
FuncTest
Generate dlltest. dll after compilation
Create a Win32 console project to call dlltest. dll.
Copy dlltest. dll to the Debug directory of Win32.
The dll. cpp file in Win32 project is as follows:
[Cpp]
# Include <iostream>
# Include "string"
# Include <stdio. h>
# Include <windows. h>
Using namespace std;
Int main ()
{
Typedef int (* HFUNC) (int );
HINSTANCE hDLL = LoadLibrary ("testmydll. dll ");
If (hDLL)
{
HFUNC hFun = (HFUNC) GetProcAddress (hDLL, "FuncTest ");
If (hFun)
{
Int a = 1; www.2cto.com
Int B = hFun ();
Printf ("% d \ n", B );
}
}
}
During compilation and execution, dlltest. dll is called to print the 100