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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZHANGKUN35268/article/details/39642379

先写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);
}

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

猜你喜欢

转载自blog.csdn.net/ZHANGKUN35268/article/details/39642379
MFC
今日推荐