Create a dynamic library under 1.MFC
1> explicitly called
Add an interface function to a. cpp file
1 int sum ( int a,int b) 2 3 return a + b; 4 5 6 int Sub (int a, b) 7 { 8 return a- b; 9 }
Marking the export interface in a. def file
1 sum @1; 2 Sub @2;
Compile to generate a dynamic library
Called in the. exe
Add in header file
1 #pragma comment (lib, "Testdll")2int sum (int A,int b); 3 int Sub (int A,int b);
1 Public : 2 void Initall (); 3 CString dectostr (int dec); 4 5 Public : 6 CString str1,str2; 7 int A, b;
Button Event
1 void Ctestdlgdlg::onbuttonadd () 2 3 // Todo:add your control notification Han Dler code here 4 Initall (); 5 int C = 6 CString sum1 = Dectostr (c); 7 Setdlgitemtext (IDC_EDIT3,SUM1); 8 }
1 void 2{3 // Todo:add your control notification handler code here 4 Initall (); 5 int d = Sub (A, b); 6 CString Sub = dectostr (d); 7 Setdlgitemtext (idc_edit4,sub); 8 }
1 voidCtestdlgdlg::initall ()2 {3 UpdateData (TRUE);4 GetDlgItemText (IDC_EDIT1,STR1);5 GetDlgItemText (IDC_EDIT2,STR2);6A =atoi (str1);7b =atoi (str2);8 }9 TenCString Ctestdlgdlg::d ectostr (intDec) One { A CString str; -Str. Format (_t ("%d"), DEC); - returnstr; the}
Then copy the appropriate. dll and. Lib to the appropriate directory.
2> Implicit invocation
Add in header file
1 typedef INT (*PFUNC) (int,int);
1 hmodule hdlllib; 2 PFUNC m_psum; 3 PFUNC m_psub;
Added in bool Ctestdlgdlg::oninitdialog ()
1Hdlllib = LoadLibrary ("TestDll.dll");2 if(Hdlllib = =NULL)3 {4AfxMessageBox ("DLL Load Error");5 returnFALSE;6 }7M_psum = (PFUNC) (GetProcAddress (Hdlllib,"sum"));8M_psub = (PFUNC) (GetProcAddress (Hdlllib,"Sub"));
Other corresponding Code
1 voidCtestdlgdlg::onbuttonadd ()2 {3 //Todo:add your control notification handler code here4 Initall ();5 intC=M_psum (A, b);6CString sum1 =Dectostr (c);7 Setdlgitemtext (IDC_EDIT3,SUM1);8 }9 Ten voidctestdlgdlg::onbuttonsub () One { A //Todo:add your control notification handler code here - Initall (); - intD =M_psub (A, b); theCString Sub =Dectostr (d); - Setdlgitemtext (idc_edit4,sub); - } - + voidCtestdlgdlg::initall () - { + UpdateData (TRUE); A GetDlgItemText (IDC_EDIT1,STR1); at GetDlgItemText (IDC_EDIT2,STR2); -A =atoi (str1); -b =atoi (str2); - } - - inCString Ctestdlgdlg::d ectostr (intDec) - { to CString str; +Str. Format (_t ("%d"), DEC); - returnstr; the}
Dynamic library generation and invocation under 2.win32
Add in the dynamic library header file
1 extern " C " int sum (int A,int b); 2 extern " C " WIN3
int Sub (int A,int b);
Added in the. cpp file
1 extern "C"Win32dll_apiintSumintAintb)2 {3 returnA +b;4 }5 6 extern "C"Win32dll_apiintSubintAintb)7 {8 returnAb;9}
Compile to generate a dynamic library
Call
Add in the. exe header file
1 #pragma comment (lib, "Win32dll")23extern"C" int sum (int A,int b); 4 extern " C " int Sub (int A,int b);
Other corresponding files
1 voidCtestdlgdlg::onbuttonadd ()2 {3 //Todo:add your control notification handler code here4 Initall ();5 intc =sum (A, b);6CString sum1 =Dectostr (c);7 Setdlgitemtext (IDC_EDIT3,SUM1);8 }9 Ten voidctestdlgdlg::onbuttonsub () One { A //Todo:add your control notification handler code here - Initall (); - intD =Sub (A, b); theCString Sub =Dectostr (d); - Setdlgitemtext (idc_edit4,sub); - } - + voidCtestdlgdlg::initall () - { + UpdateData (TRUE); A GetDlgItemText (IDC_EDIT1,STR1); at GetDlgItemText (IDC_EDIT2,STR2); -A =atoi (str1); -b =atoi (str2); - } - - inCString Ctestdlgdlg::d ectostr (intDec) - { to CString str; +Str. Format (_t ("%d"), DEC); - returnstr; the}
The above code passes the test!