1.MFC下生成动态库
1>显式调用
在.cpp文件里添加接口函数
1 int sum(int a,int b) 2 { 3 return a + b; 4 } 5 6 int sub(int a,int b) 7 { 8 return a - b; 9 }
在.def文件里标示导出接口
1 sum @1; 2 sub @2;
编译即可生成动态库
在.exe里调用
在头文件里添加
1 #pragma comment(lib,"TestDLL") 2 int 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事件
1 void CTestDlgDlg::OnButtonAdd() 2 { 3 // TODO: Add your control notification handler code here 4 InitAll(); 5 int c = sum(a,b); 6 CString sum1 = decToStr(c); 7 SetDlgItemText(IDC_EDIT3,sum1); 8 }
1 void CTestDlgDlg::OnButtonSub() 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 void CTestDlgDlg::InitAll() 2 { 3 UpdateData(TRUE); 4 GetDlgItemText(IDC_EDIT1,str1); 5 GetDlgItemText(IDC_EDIT2,str2); 6 a = atoi(str1); 7 b = atoi(str2); 8 } 9 10 CString CTestDlgDlg::decToStr(int dec) 11 { 12 CString str; 13 str.Format(_T("%d"),dec); 14 return str; 15 }
然后将相应的.dll和.lib拷贝到相应目录即可。
2>隐式调用
在头文件里添加
1 typedef int (*PFUNC)(int,int);
1 HMODULE hDllLib; 2 PFUNC m_pSum; 3 PFUNC m_pSub;
在BOOL CTestDlgDlg::OnInitDialog()里添加
1 hDllLib = LoadLibrary("TestDll.dll"); 2 if (hDllLib == NULL) 3 { 4 AfxMessageBox("dll load error"); 5 return FALSE; 6 } 7 m_pSum = (PFUNC)(GetProcAddress(hDllLib,"sum")); 8 m_pSub = (PFUNC)(GetProcAddress(hDllLib,"sub"));
其他相应代码
1 void CTestDlgDlg::OnButtonAdd() 2 { 3 // TODO: Add your control notification handler code here 4 InitAll(); 5 int c= m_pSum(a,b); 6 CString sum1 = decToStr(c); 7 SetDlgItemText(IDC_EDIT3,sum1); 8 } 9 10 void CTestDlgDlg::OnButtonSub() 11 { 12 // TODO: Add your control notification handler code here 13 InitAll(); 14 int d = m_pSub(a,b); 15 CString Sub = decToStr(d); 16 SetDlgItemText(IDC_EDIT4,Sub); 17 } 18 19 void CTestDlgDlg::InitAll() 20 { 21 UpdateData(TRUE); 22 GetDlgItemText(IDC_EDIT1,str1); 23 GetDlgItemText(IDC_EDIT2,str2); 24 a = atoi(str1); 25 b = atoi(str2); 26 } 27 28 29 CString CTestDlgDlg::decToStr(int dec) 30 { 31 CString str; 32 str.Format(_T("%d"),dec); 33 return str; 34 }
2.Win32下动态库生成及调用
动态库头文件里添加
1 extern "C" WIN32DLL_API int sum(int a,int b); 2 extern "C" WIN3
2DLL_API int sub(int a,int b);
.cpp文件里添加
1 extern "C" WIN32DLL_API int sum(int a,int b) 2 { 3 return a + b; 4 } 5 6 extern "C" WIN32DLL_API int sub(int a,int b) 7 { 8 return a - b; 9 }
编译即生成动态库
调用
在.exe头文件里添加
1 #pragma comment(lib,"Win32DLL") 2 3 extern "C" int sum(int a,int b); 4 extern "C" int sub(int a,int b);
其他相应文件
1 void CTestDlgDlg::OnButtonAdd() 2 { 3 // TODO: Add your control notification handler code here 4 InitAll(); 5 int c = sum(a,b); 6 CString sum1 = decToStr(c); 7 SetDlgItemText(IDC_EDIT3,sum1); 8 } 9 10 void CTestDlgDlg::OnButtonSub() 11 { 12 // TODO: Add your control notification handler code here 13 InitAll(); 14 int d = sub(a,b); 15 CString Sub = decToStr(d); 16 SetDlgItemText(IDC_EDIT4,Sub); 17 } 18 19 void CTestDlgDlg::InitAll() 20 { 21 UpdateData(TRUE); 22 GetDlgItemText(IDC_EDIT1,str1); 23 GetDlgItemText(IDC_EDIT2,str2); 24 a = atoi(str1); 25 b = atoi(str2); 26 } 27 28 29 CString CTestDlgDlg::decToStr(int dec) 30 { 31 CString str; 32 str.Format(_T("%d"),dec); 33 return str; 34 }
以上代码通过测试!