MFC 动态dll调用函数编码实现
一、
1.先打开Microsoft Visual Studio 2010软件。
2.点击新建项目->选择MFC DLL,在名称处填写“DllTest”,点击确定。
3.点击下一步->选择“使用共享MFC DLL的规则 DLL”,点击完成。
4.在“DllTest.h”文件中,添加extern “C” __declspec(dllexport)bool add(int a,int b,int *c);语句。
5.在“DllTest.def”文件中,在输入add。
6.在“DllTest.cpp”文件中,添加如下语句:
extern "C" __declspec(dllexport)bool add(int a,int b,int *c)
{
*c=a+b;
return true;
}
修改后的这三个文件为:
DllTest.h
// DllTest.h : main header file for the DllTest DLL // #pragma once #ifndef __AFXWIN_H__ #error "include 'stdafx.h' before including this file for PCH" #endif #include "resource.h" // main symbols // CDllTestApp // See DllTest.cpp for the implementation of this class // class CDllTestApp : public CWinApp { public : CDllTestApp(); // Overrides public : virtual BOOL InitInstance(); DECLARE_MESSAGE_MAP() }; extern "C" __declspec ( dllexport ) bool add( int a, int b, int *c); |
DllTest.def
; DllTest.def : Declares the module parameters for the DLL. LIBRARY EXPORTS ; Explicit exports can go here add |
DllTest.cpp
// DllTest.cpp : Defines the initialization routines for the DLL. // #include "stdafx.h" #include "DllTest.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // //TODO: If this DLL is dynamically linked against the MFC DLLs, // any functions exported from this DLL which call into // MFC must have the AFX_MANAGE_STATE macro added at the // very beginning of the function. // // For example: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // normal function body here // } // // It is very important that this macro appear in each // function, prior to any calls into MFC. This means that // it must appear as the first statement within the // function, even before any object variable declarations // as their constructors may generate calls into the MFC // DLL. // // Please see MFC Technical Notes 33 and 58 for additional // details. // // CDllTestApp BEGIN_MESSAGE_MAP(CDllTestApp, CWinApp) END_MESSAGE_MAP() // CDllTestApp construction CDllTestApp::CDllTestApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } // The one and only CDllTestApp object CDllTestApp theApp; // CDllTestApp initialization BOOL CDllTestApp::InitInstance() { CWinApp::InitInstance(); return TRUE; } extern "C" __declspec(dllexport)bool add(int a,int b,int *c) { *c=a+b; return true; }
编译生成DllTest.dll
二、再新建一个基于对话框的工程,再ok按钮事件函数中添加如下代码:
void CCreateThreadDlg::SetEditText(CString strText) { GetDlgItem(IDC_EDIT1)->SetWindowText(strText); } void CCreateThreadDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //CDialogEx::OnOK(); HINSTANCE hPro= ::LoadLibrary("DllTest.dll"); if(hPro == NULL) { return; } else { typedef bool (*DLL_ADD)( int a,int b,int *c); DLL_ADD dllAddFunc = NULL; dllAddFunc=(DLL_ADD)::GetProcAddress(hPro, "add"); if(dllAddFunc) { int s= 0; dllAddFunc(8,4,&s); CString strSum; strSum.Format("%d", s); SetEditText(strSum); } ::FreeLibrary(hPro); } }
再把DllTest.dll复制到测试工程的exe同级目录,然后编译、运行,对话框中的输入框值为12
分类:
MFC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义