1、动态库的编写
(1)编写外部接口的时候必须添加 “extern “C” ”
如:
extern "C"
{
int __stdcall init(const TCHAR *dbfile);
int __stdcall end();
}
(2)用VS编译的时候配置属性-常规-配置类型选择 动态库(.dll)
2、动态库的使用
(1)先使用 typedef 对要使用的函数起个别名
typedef int (*init_)(const TCHAR *dbfile);
(2)加载动态库,主要使用两个函数
//加载动态库,filename是动态库的名字
gdf_base = LoadLibrary(filename);
//通过GetProcAddress函数获取动态库的接口地址,从而进行使用
init = (init_)GetProcAddress(gdf_base, "init");
例子(动态库):
common.h
#include <stdio.h>
extern "C"
{
int __stdcall init(const TCHAR *dbfile);
}
common.cpp
#include "common.h"
int init(const TCHAR *dbfile)
{
printf("hello world!\n");
return 0;
}
例子(使用动态库):
test.cpp
typedef int ( *init_)(const TCHAR *dbfile);
int main()
{
HMODULE gdf_base = LoadLibrary(libcommon.dll));
if (!gdf_base)
return 1;
init_ init = (init_)GetProcAddress(gdf_base, "init");
init(NULL);
return 0;
}