MFC 公用函数和函数指针和动态库

--------------------生成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);
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值