1、DLL源代码
MyDll.h
- //////////////////////////////////////////////////////////////////////////
- // MyDll.h
- // 声明函数
- int _stdcall Add(int a,int b);
- int _stdcall Sub(int a,int b);
MyDll.cpp
- //////////////////////////////////////////////////////////////////////////
- // MyDll.pp
- // 声明实现
- #include "MyDll.h"
- int _stdcall Add(int a,int b)
- {
- return a+b;
- }
- int _stdcall Sub(int a,int b)
- {
- return a-b;
- }
MyDll.def
- ; MyDll为工程名
- LIBRARY MyDll
- ; 在这里声明需要导出的函数
- EXPORTS
- Add
- Sub
2、Exe测试代码
演示动态与静态加载的方法,看代码吧!
- void CTestDlg::OnBtnStatic()
- {
- // TODO: Add your control notification handler code here
- // 静态加载的方法:
- // 1、添加头文件 #include "MyDll.h"
- // 2、引入Lib库 #pragma comment(lib,"MyDll.lib")
- // 3、这样就可以直接使用MyDll.h中导入的函数
- CString str;
- str.Format("静态加载: 1+1=%d 1-1=%d",Add(1,1),Sub(1,1));
- MessageBox(str);
- }
- void CTestDlg::OnBtnDynamic()
- {
- // TODO: Add your control notification handler code here
- // 动态加载的方法:
- // 不需要引入头文件与lib文件,仅需要一个dll即可
- // 注意这里的条约调用约定_stdcall不要忘记加(不然会引会esp出错)
- typedef int (_stdcall *ADDPROC)(int,int);
- typedef int (_stdcall *SUBPROC)(int,int);
- HINSTANCE handle;
- handle = LoadLibrary("MyDll.dll");
- if(handle)
- {
- // GetProcAddress第二个参数有两种方法:
- // 1、通过DLL中的函数名
- // 2、通过Depend工具中Ordinal索引值来查看
- ADDPROC MyAdd = (ADDPROC)GetProcAddress(handle,"Add");
- SUBPROC MySub = (ADDPROC)GetProcAddress(handle,MAKEINTRESOURCE(2));
- if( !MyAdd )
- {
- MessageBox("函数Add地址获取失败!");
- return;
- }
- if( !MySub )
- {
- MessageBox("函数Sub地址获取失败!");
- return;
- }
- CString str;
- str.Format("动态加载: 1+1=%d 1-1=%d",MyAdd(1,1),MySub(1,1));
- MessageBox(str);
- }
- FreeLibrary(handle);
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!