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 );}