一个c++动态链接库为:bjcast_sender_wrapper.dll其对外的接口头文件为bjcast_sender_itf.h,那么怎么在addon中调用dll库对应的函数呢?
bjcast_sender_itf.h提供的接口函数如下:
CONTROLMANAGER_WRAPPER_API S32 BJCastLogin(ControlManagerIntfHandle handle, const S8 *ip, U16 port, const S8* pin = "");
addon中的调用方式如下:
1. 首先声明函数指针类型
typedef S32(_cdecl*BJCastLoginFun)(ControlManagerIntfHandle, const S8*, U16, const S8*);
该声明对应bjcast_sender_itf.h中的接口
参数说明:
S32 – 对应BJCastLogin函数的返回值。
BJCastLoginFun – 自己命名后面会使用到。
(ControlManagerIntfHandle, const S8*, U16, const S8*) – 对应BJCastLogin接口里面传参的参数类型。
2. 加载动态链接库
init(){
// 设置dll的查找路径
char path[MAX_PATH + 30];
char cur_path[MAX_PATH];
char sdk_path[30] = "BJSDK\\windows\\lib_32";
GetCurrentDirectory(MAX_PATH, cur_path);
strncpy(path, cur_path, MAX_PATH);
strcat(path, "\\");
strncat(path, sdk_path, MAX_PATH);
SetDllDirectory(path); //上面只是设置dll的查找路径,也可以直接在此处把path替换为绝对路径
HINSTANCE bjcast_sender_wrapper_dll =
LoadLibrary("bjcast_sender_wrapper.dll");//加载动态库获取句柄指针
if (bjcast_sender_wrapper_dll != NULL) {
//获取函数入口地址
login_fun = (BJCastLoginFun)GetProcAddress(bjcast_sender_wrapper_dll,
"BJCastLogin");
}
}
3. 调用dll
可以直接在addon里面通过上面获取的login_fun函数指针调用底层的dll。
login_fun(_libContext->libHander, ip, port, pass)