MFC APP中使用MFC DLL(用C++)

先写DLL文件:

选择MFC扩展DLL。

在.def文件中加入:

; mfcDBstore_1.def : Declares the module parameters for the DLL.

LIBRARY      "mfcDBstore_1"
DESCRIPTION  'mfcDBstore_1 Windows Dynamic Link Library'

EXPORTS
    ; Explicit exports can go here
 mfcDBstore;      //这个是需要导出的名字

 

然后再CPP文件中加入:

void APIENTRY mfcDBstore(); //extern "C" void APIENTRY Msg();       //去掉extern,以C++的方式编译

然后:

class A            //写自己需要的类
{
public:
 A()
 {
  AfxMessageBox("A()");
 }
 ~A()
 {
  AfxMessageBox("~A()");
 }
};
void APIENTRY mfcDBstore()       //创建对象
{
 AfxMessageBox("click mouse mfcDBstore");
 A a;

}

至此,DLL编写完。开始编写MFC 单文档程序:

选择类视图,添加消息处理函数,选择鼠标左键按下。

添加代码:

void CMfcDBstore_2View::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 FARPROC  lpfn;     //定义函数地址
 HINSTANCE hinst;    //定义句柄
 hinst=LoadLibrary("C:/Program Files/Microsoft Visual Studio/MyProjects/DLLspace2/mfcDBstore_1/Debug/mfcDBstore_1.dll");//载入DLL
 if(hinst==NULL)      //载入失败
 {
  AfxMessageBox("载入DLL失败!");
  return;//返回
 }
 lpfn=GetProcAddress(hinst,"mfcDBstore");//取得DLL导出函数的地址
 if (lpfn==NULL)
 {
  AfxMessageBox("读取函数地址失败!");
  return;//返回
 }  
 lpfn();//执行DLL函数
 FreeLibrary(hinst);//释放DLLShareMFCDill.dll
 
 CView::OnLButtonDown(nFlags, point);
}

嗯。按下鼠标就可以看到  默认的构造和析构被调用。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值