[转]C/C++中动态链接库的创建和调用

转载自:http://developer.51cto.com/art/200702/39608.htm

动态连接库的创建步骤:

一、创建Non-MFC DLL动态链接库

1、打开File —> New —> Project选项,选择Win32 Dynamic-Link Library —>sample project —>工程名:DllDemo

2、新建一个.h文件DllDemo.h

#ifdef DllDemo_EXPORTS
#define DllAPI __declspec(dllexport)
#else
#define DllAPI __declspec(dllimport)
extern "C" //原样编译
{
DllAPI int __stdcall Max(int a,int b); //__stdcall使非C/C++语言内能够调用API
}
#endif

3、在DllDemo.cpp文件中导入DllDemo.h文件,并实现Max(int,int)函数

#include "DllDemo.h"
DllAPI int __stdcall Max(int a,int b)
{
if(a==b)
return NULL;
else if(a>b)
return a;
else
return b;
}

4、编译程序生成动态连接库

二、用.def文件创建动态连接库DllDemo.dll

1、删除DllDemo工程中的DllDemo.h文件。

2、在DllDemo.cpp文件头,删除 #include DllDemo.h语句。

3、向该工程中加入一个文本文件,命名为DllDemo.def并写入如下语句:

LIBRARY MyDll

EXPORTS

Max@1

4、编译程序生成动态连接库。

动态链接的调用步骤:

一、隐式调用

1、建立DllCnslTest工程

2、将文件DllDemo.dll、DllDemo.lib拷贝到DllCnslTest工程所在的目录

3、在DllCnslTest.h中添加如下语句:

#define DllAPI __declspec(dllimport)
#pragma comment(lib,"DllDemo.lib") //在编辑器link时,链接到DllDemo.lib文件
extern "C"
{
DllAPI int __stdcall Max(int a,int b);
}

4、在DllCnslTest.cpp文件中添加如下语句:

#include "DllCnslTest.h"//或者 #include "DllDemo.h"
void main()
{
int value;
value = Max(2,9);
printf("The Max value is %d/n",value);
}

5、编译并生成应用程序DllCnslTest.exe

二、显式调用

1、建立DllWinTest工程。

2、将文件DllDemo.dll拷贝到DllWinTest工程所在的目录或Windows系统目录下。

3、用vc/bin下的Dumpbin.exe的小程序,查看DLL文件(DllDemo.dll)中的函数结构。

4、使用类型定义关键字typedef,定义指向和DLL中相同的函数原型指针。

例:

typedef int(*lpMax)(int a,int b); //此语句可以放在.h文件中

5、通过LoadLibray()将DLL加载到当前的应用程序中并返回当前DLL文件的句柄。

例:

HINSTANCE hDll; //声明一个Dll实例文件句柄
hDll = LoadLibrary("DllDemo.dll");//导入DllDemo.dll动态连接库

6、通过GetProcAddress()函数获取导入到应用程序中的函数指针。

例:

lpMax Max;
Max = (lpMax)GetProcAddress(hDLL,"Max");
int value;
value = Max(2,9);
printf("The Max value is %d",value);

7、函数调用完毕后,使用FreeLibrary()卸载DLL文件。

FreeLibrary(hDll);

8、编译并生成应用程序DllWinTest.exe

注:显式链接应用程序编译时不需要使用相应的Lib文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态链接库是一种可重定位的二进制文件,它包含了一些可供其他程序调用的函数或数据。在 Windows 平台上,动态链接库采用 .dll 后缀名,而在 Linux 平台上则采用 .so 后缀名。在本文,我们将介绍如何使用 MFC 调用 C 语言编写的动态链接库。 # 创建动态链接库Windows 平台上,可以使用 Visual Studio 创建动态链接库下面是一个简单的示例: ```c // mydll.h #ifndef MYDLL_H #define MYDLL_H #ifdef __cplusplus extern "C" { #endif __declspec(dllexport) int add(int a, int b); #ifdef __cplusplus } #endif #endif // MYDLL_H // mydll.c #include "mydll.h" int add(int a, int b) { return a + b; } ``` 这个动态链接库包含一个 add 函数,可以对两个整数求和。 # 调用动态链接库 在 MFC 项目调用动态链接库,需要进行以下几个步骤: 1. 定义一个函数指针类型,指向动态链接库的函数。 ```c++ typedef int (*AddFunc)(int, int); ``` 2. 加载动态链接库。 ```c++ HINSTANCE hinstLib = LoadLibrary(TEXT("mydll.dll")); if (hinstLib == NULL) { AfxMessageBox(TEXT("Failed to load library.")); return; } ``` 3. 获取动态链接库的函数地址。 ```c++ AddFunc addFunc = (AddFunc)GetProcAddress(hinstLib, "add"); if (addFunc == NULL) { AfxMessageBox(TEXT("Failed to get function address.")); FreeLibrary(hinstLib); return; } ``` 4. 调用动态链接库的函数。 ```c++ int result = addFunc(1, 2); CString str; str.Format(TEXT("1 + 2 = %d"), result); AfxMessageBox(str); ``` 5. 卸载动态链接库。 ```c++ FreeLibrary(hinstLib); ``` 完整的 MFC 代码示例: ```c++ typedef int (*AddFunc)(int, int); void CMyDlg::OnButton1() { HINSTANCE hinstLib = LoadLibrary(TEXT("mydll.dll")); if (hinstLib == NULL) { AfxMessageBox(TEXT("Failed to load library.")); return; } AddFunc addFunc = (AddFunc)GetProcAddress(hinstLib, "add"); if (addFunc == NULL) { AfxMessageBox(TEXT("Failed to get function address.")); FreeLibrary(hinstLib); return; } int result = addFunc(1, 2); CString str; str.Format(TEXT("1 + 2 = %d"), result); AfxMessageBox(str); FreeLibrary(hinstLib); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值