1. Create a dynamic link library DLL3
- The code for DLL3.cpp is as follows
#include <iostream>using namespace STD;voidPro1 (intAint(*FUN1) (intb)) {if(A > -) {Fun1 (a); }Else{cout<<"Do not call other functions"<< Endl; }}intFUN3 (intA) {cout<<"This number is:"<< a << Endl;return 0;}int(*FUN4) (inta);voidPro2 (intAvoid(*pro) (intBint(*FUN2) (intc)),int(*fun) (intD)) {if(A >0) {Pro (A, fun); }Else{cout<<"A is not greater than 0"<< Endl; }}
- The code for DLL3.DEF is as follows
LIBRARY DLL3EXPORTSPro1Pro2
2. Building a test DLL project TestDll3
/**@ to test the use of callback functions outside the DLL * /#include <iostream>using namespace STD;#pragma comment (lib, "DLL3.lib")_declspec (dllimport)voidPro1 (intAint(*FUN1) (intb)); _declspec (dllimport)voidPro2 (intAvoid(*pro) (intBint(*FUN2) (intc)),int(*fun) (intd));intFun5 (intb) {cout<<"This value is:"<< b << Endl;return 0;}intMain () {//pro1 (200,fun);Pro2 (1, PRO1,FUN5); Pro2 (101, Pro1, FUN5); System"Pause");return 0;}
3. Summary
Because of the needs of the project, you need to build a DLL, and you need to call the callback function to write the database outside of the dynamic link library, after 1 days of pondering the above procedure. The biggest highlight is
(1) The exported two functions are related, PRO1 can be used as parameters of Pro2
(2) The formal parameter of the PRO1 function is the callback function (and in the outside of the DLL, the FUN5 in TestDll3)
(3) Implement a function with complex parameters such as void Pro2 (int a, void (*pro) (int b,int (*FUN2) (int c)), int (*fun) (int d))
Dynamic link library (4) How the callback function is handled outside of the dynamic link library