標籤:style blog color os io 檔案 for ar
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 here4 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 here4 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 here13 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 here13 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 }
以上代碼通過測試!