C/C++中动态链接库的创建和调用
动态连接库的创建步骤:
一、创建 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 语句。