Dynamic link library (4) How the callback function is handled outside of the dynamic link library

Source: Internet
Author: User

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
    • The code is as follows
/**@ 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;}
    • Experimental results
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.