--------------------生成dll-------------------
// AddDLL.h: interface for the AddDLL class.
//
//
#ifndef ADDDLL_H
#define ADDDLL_H
extern "C" int __declspec(dllexport)add(int x, int y);
#endif
// AddDLL.cpp:
#include "AddDLL.h"
//
// Construction/Destruction
//
int add(int x, int y)
{
return x + y;
}
--------------------调用dll-------------------
void CTestAddDLLDlg::OnAdd()
{
// TODO: Add your control notification handler code here
UpdateData(true);
typedef int(*lpAddFun)(int, int); //宏定义函数指针类型
HINSTANCE hDll; //DLL句柄
lpAddFun addFun; //函数指针
hDll = LoadLibrary("..\\Debug\\AddDLL.dll");
if (hDll != NULL)
{
addFun = (lpAddFun)GetProcAddress(hDll, "add");
if (addFun != NULL)
{
int result = addFun(m_X, m_Y);
CString s;
s.Format("调用成功,结果为:%d",result);
MessageBox(s);
}
FreeLibrary(hDll);
}
}
CString itos(int i)
{
CString str;
str.Format("%d",i);
return str ;
}
void CTestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
int i = 5 ;
AfxMessageBox(itos(i));
}
/
//第一步:在类成员中或者全局成员声明回调指针
typedef void ( *CallBackDelegate)(CObject * ,CDC* , CPoint );//回调函数
//第二步:静态成员函数声明定义
static void Fun_OnDraw(CObject* Obj , CDC* pDC, CPoint pt)
{
CDimension* p = (CDimension*)Obj;
p->OnDraw(pDC);
}
//第三步:遍历链表内的元素
void TraverFun(CallBackDelegate FunCallBack , CDC* pDC , CPoint pt)
{
if(!m_List.IsEmpty())
{
POSITION pos = COperationList::m_List.GetHeadPosition() ;
while(pos != NULL)
{
CObject *p = (CObject*)COperationList::m_List.GetNext(pos);
FunCallBack(p,pDC,pt);
}
}
}
//第四步:调用传参
CDC* pDC = GetDC();
Ope.TraverFun(COperationList::Fun_OnDraw , pDC , NULL);