如何调用 DLL 中的函数

2025-04-21 19:14:08

1、在 DLL工程中的 cpp中函数定义如下:extern "C" _declspec (dllexport ) int fun(int a, char b){ return a + b;}

2、第一种方法 隐式调用:调用的 DLL的主工程的 文件中代码如下:// 先把 lib 链接进来#pragma comment (lib , "..//Debug//FuncDll.lib" )// 外部声明的 add 函数extern "C" _declspec (dllimport ) int fun(int a, char b);int TestDll(){ // 直接调用 fun函数 printf("%d/n" , fun(5, 2)); return 0;}

3、第二种方法 显式调用:调用的 DLL的主工程的 文件中代码如下:int TestDLL()辘腋粪梯{ HMODULE hModule = NULL; t烤恤鹇灭ypedef int (*Func)(int a, int b); // 动态加载 DLL 文件 hModule = LoadLibrary(_TEXT("..//Debug//FuncDll.dll" )); // 获取 fun函数地址 Func fAdd = (Func)GetProcAddress(hModule, "fun" ); // 使用函数指针 printf("%d/n" , fAdd(3, 1)); // 释放指针 FreeLibrary(hModule); return 0;}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢