动态库的编写和使用(windows下)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值