转自:http://bbs.pediy.com/showthread.php?t=56840
EXE导出函数三种不同的解决办法(原理可能是一样的,:)):
1)在导出函数声明之前加上__declspec(dllexport)。例:
__declspec(dllexport) int Add(int a, int b); __declspec(dllexport) int Sub(int a, int b); __declspec(dllexport) int Mul(int a, int b); __declspec(dllexport) int Div(int a, int b);
#pragma comment(linker, "/EXPORT:_Add,@1,NONAME") #pragma comment(linker, "/EXPORT:_Sub,@2,NONAME") #pragma comment(linker, "/EXPORT:_Mul,@3,NONAME") #pragma comment(linker, "/EXPORT:_Div,@4,NONAME")
EXPORTS Add @1 NONAME Sub @2 NONAME Mul @3 NONAME Div @4 NONAME
函数举例:
extern "C" { int Add(int a, int b) { return (a + b); } int Sub(int a, int b) { return (a - b); } int Mul(int a, int b) { return (a * b); } int Div(int a, int b) { if (b == 0) return 0; else return (a / b); } }编译时会自动生成相应的导出库(lib)文件,供调用者使用。
extern "C" { int Add(int a, int b); int Sub(int a, int b); int Mul(int a, int b); int Div(int a, int b); } #pragma comment (lib, "Callee.lib") void CCallerDlg::OnBnClickedCalculate() { // TODO: Add your control notification handler code here UpdateData(TRUE); switch (((CComboBox *)GetDlgItem(IDC_COMBO_OPERATOR))->GetCurSel()) { case ADD: { m_iResult = Add(m_iNum1, m_iNum2); break; } case SUB: { m_iResult = Sub(m_iNum1, m_iNum2); break; } ... ...
根据导出序号调用dll函数
GetProcAddress(hDll,MAKEINTRESOURCE(sequence_num));
另外的方式:《编程高手箴言》一书所述调用门
//------------------------------------------------------------------------------------------------------------------------
def文件说明传送门:
http://andylin02.iteye.com/blog/441300