如何调用DLL中的函数

 


标签:  dll null c
2011-06-09 20:24  8693人阅读  评论(0)  收藏  举报

版权声明:本文为博主原创文章,未经博主允许不得转载。

如何调用 DLL 中的函数

 

在 DLL工程中的 cpp中函数定义如下:

extern "C" _declspec (dllexport )

      int add(int a, char b)

{

      return a + b;

}

 

 

一:显示链接

调用的 DLL的主工程的 main文件中代码如下:

#include <stdio.h>

#include <Windows.h>

#include <tchar.h>

 

int main()

{

      HMODULE hModule = NULL;

      typedef int (*Func)(int a, int b);

 

      // 动态加载 DLL 文件

      hModule = LoadLibrary(_TEXT("..//Debug//FuncDll.dll" ));

 

      // 获取 add 函数地址

      Func fAdd = (Func)GetProcAddress(hModule, "add" );

 

      // 使用函数指针

      printf("%d/n" , fAdd(5, 2));

 

      // 最后记得要释放指针

      FreeLibrary(hModule);

 

      return 0;

}

 

二:隐式链接:

调用的 DLL的主工程的 main文件中代码如下:

#include <stdio.h>

#include <Windows.h>

#include <tchar.h>

 

// 先把 lib 链接进来

#pragma comment (lib , "..//Debug//FuncDll.lib" )

 

// 外部声明的 add 函数

extern "C" _declspec (dllimport )

      int add(int a, char b);

 

int main()

{

      // 直接调用 add 函数

      printf("%d/n" , add(5, 2));

 

      return 0;

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值