如何调用 DLL 中的函数

 时间:2026-02-12 11:59:28

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;

      typedef 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;

}

  • window技巧:[5]BAT文件实现补丁批量的安装
  • 联想拯救者r720怎么进入bios界面
  • 怎么编辑注册表
  • Windows10系统打开管理员命令提示符的六种方法
  • Windows11重设管理终端目录方法介绍
  • 热门搜索
    孔子简介50字 暴走大侠攻略 减肥视频 春笋怎么做好吃 韩国旅游攻略 绿茶减肥吗 张静静个人资料简介 好用的减肥药 苹果mac地址怎么查 家庭红烧肉的简单做法