Create DLL dynamic link library function callback mfc dll in VC

Source: Internet
Author: User

Create a Win32 DLL or an MFC DLL

You can manually add a. h file for Win32 DLL, which corresponds to the corresponding CPP file, as shown in the following demo.

// Demodll2.cpp: defines the export function of the DLL application. // # Include "stdafx. H "# include" demodll2.h "# include" stdio. H "# define mylibapi extern" C "_ declspec (dllexport) void sayhello (char * Name) {printf (" C code start... \ n "); printf (" Hello! Mr % S. \ n ", name); printf (" C code end. \ n ");}

// Demodll2.h: defines the export function of the DLL application. # Ifdef mylibapi # else # define mylibapi extern "C" _ declspec (dllimport) # endifmylibapi void sayhello (char * Name );

========================================================== ====

Implementation of DLL function callback based on MFC

// Callbackdll. h: Main header file of callbackdll // # pragma once # ifndef _ afxwin_h__# error "contains" stdafx. H "to generate the PCH file" # endif # include "resource. H "// main symbol # ifdef mylibapi # else # define mylibapi extern" C "_ declspec (dllimport) # endif typedef int (* mycallback) (long USER, int, int B); // ccallbackdllapp // For information about this implementation, see callbackdll. CPP // class ccallbackdllapp: Public cwinapp {public: ccallbackdllapp (); // rewrite public: Virtual bool initinstance (); declare_message_map ()}; mylibapi int dlladd (int, int B); // long user calls the DLL window pointer mylibapi void setcallfunction (long USER, INT (* Add) (long, Int, INT ));

// Callbackdll. cpp: defines the DLL initialization routine. // # Include "stdafx. H "# include" callbackdll. H "# ifdef _ debug # define new debug_new # endif # define mylibapi extern" C "_ declspec (dllexport) // invoke (ccallbackdllapp, cwinapp) end_message_map () // ccallbackdllapp construct ccallbackdllapp: ccallbackdllapp () {// todo: add the construction code here, // place all important initialization tasks in initinstance} // the unique ccallbackdllapp object ccallbackdllapp theapp; // ccallbackdllapp initializes bool ccallbackdllapp: initinstance () {cwinapp :: initinstance (); Return true;} mycallback myadd; long duser; void setcallfunction (long USER, INT (* Add) (long, Int, INT) {myadd = add; duser = user;} int dlladd (int A, int B) {int x = myadd (duser, a, B); Return X ;}

// Test the dll mfc Program

Int myadd (long USER, int A, int B) {cdlltestdlg * DLG = (cdlltestdlg *) user; // forcibly convert a user to a pointer of the class cstring Cs; CS. format ("A = % d, B = % d", a, B); DLG-> MessageBox ("Callback successful! \ R \ n "+ CS); DLG-> MessageBox (DLG-> temp); Return A + B;} void cdlltestdlg: onbnclickedbutton1 () {// todo: add the control notification handler code temp = "this is the test string in this"; setcallfunction (long) This, myadd ); // This pointer is passed to the callback function int x = dlladd (3, 4); cstring Cs; CS. format ("Result: % d", x); MessageBox (CS );}

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.