dlopen、dlsym和dlclose的使用

因为项目需求,需要集成一个第三方的方案。但是第三方只给我们release 了一个so 库+函数说明;现在需要调用到so 库中的函数。。

先给出一些函数的定义

       #include <dlfcn.h>

       void *dlopen(const char *filename, int flag);

       char *dlerror(void);

       void *dlsym(void *handle, const char *symbol);

       int dlclose(void *handle);

       //Link with -ldl.

这里需要注意亮点

1、加上头文件#inclue<dlfcn.h>

2、编译的时候加上 -ldl选项

下面是生成动态供调用的so库代码

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
//#define bool int

void *getValue(char *param,int intparam){
    printf("%s-->%d,param=%s,intparam=%d\n",__FUNCTION__,__LINE__,param,intparam);
    return 0;
}

void *getValue1(char *param,int intparam,int isReturnAddress){
    printf("%s-->%d,param=%s,intparam=%d,isReturnAddress=%d\n",__FUNCTION__,__LINE__,param,intparam,isReturnAddress);
    if(isReturnAddress>0){
        return 1;
    }else{
        return 0;
    }
}

void * InitEffect(unsigned char bitsPerSample,unsigned char numChannels,unsigned char dataIsInterleaved,unsigned int sampleRate){
    printf("%s-->%d\n",__FUNCTION__,__LINE__);
}
void *ReinitEffect(unsigned char bitsPerSample,unsigned char numChannels,unsigned char dataIsInterleaved,unsigned int sampleRate){
    printf("%s-->%d\n",__FUNCTION__,__LINE__);
}

int Process(void* inBuffer,void* outBuffer,void *mePtr,unsigned short num_frames){
    printf("%s-->%d\n",__FUNCTION__,__LINE__);
}
int SetParam(void *mPtr,int paramId,void* paramVal){
    printf("%s-->%d\n",__FUNCTION__,__LINE__);
}
int GetParam(void *mPtr,int paramId){
    printf("%s-->%d\n",__FUNCTION__,__LINE__);
}
int Deallocate(void * mePtr){
    printf("%s-->%d\n",__FUNCTION__,__LINE__);
}
void * GetSpecBufAddress(void *mePtr,int *bufLength){
    printf("%s-->%d\n",__FUNCTION__,__LINE__);
}

编译生成 so库

gcc -fPIC -shared -o test.so test.c
务必注意-fPIC编译选择

测试使用so库的代码

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include <dlfcn.h>

typedef void* (*TCT_InitEffect)(unsigned char,unsigned char,unsigned char,unsigned int);
typedef void* (*TCT_ReinitEffect)(unsigned char,unsigned char,unsigned char,unsigned int);
typedef int (*TCT_Process)(void*,void*,void *,unsigned short);
typedef int (*TCT_SetParam)(void*,int,void*);
typedef int (*TCT_GetParam)(void*,int);
typedef int (*TCT_Deallocate)(void*);
typedef int (*TCT_GetSpecBufAddress)(void *mePtr,int *bufLength);
typedef void* (*tct_getValue)(char*,int);
typedef void* (*tct_getValue1)(char*,int,int);

int main(int argc, char **argv) {

    //open the .so library
    void* handle = dlopen("./test.so", RTLD_LAZY);

    //get function pointer from dlsym
    tct_getValue getValue = (tct_getValue)dlsym(handle, "getValue");
    tct_getValue1 getValue1 = (tct_getValue1)dlsym(handle, "getValue1");
    TCT_InitEffect Harman_InitEffect = (TCT_InitEffect)dlsym(handle, "InitEffect");
    TCT_ReinitEffect Harman_ReinitEffect = (TCT_ReinitEffect)dlsym(handle, "ReinitEffect");
    TCT_Process Harman_Process = (TCT_Process)dlsym(handle, "Process");
    TCT_SetParam Harman_SetParam = (TCT_SetParam)dlsym(handle, "SetParam");
    TCT_GetParam Harman_GetParam = (TCT_GetParam)dlsym(handle, "GetParam");
    TCT_Deallocate Harman_Deallocate = (TCT_Deallocate)dlsym(handle, "Deallocate");
    TCT_GetSpecBufAddress Harman_GetSpecBufAddress = (TCT_GetSpecBufAddress)dlsym(handle, "GetSpecBufAddress");

    //call function
    Harman_InitEffect(1,1,1,1);
    Harman_ReinitEffect(1,1,1,1);
    Harman_Process(1,1,1,1);
    Harman_SetParam(1,1,1);
    Harman_GetParam(1,1);
    Harman_Deallocate(1);
    Harman_GetSpecBufAddress(1,1);
    getValue("getValuexxxxxxx",1);
    int xxx = getValue1("getValue1",1,0);
    printf("main function return from getVaule1 =%d\n",xxx);

    //dlclose handle
    dlclose(handle);

    return 0;
}


</pre><p></p><pre>
编译生成可执行文件

gcc hello.c  -o hello -ldl
这里需要加入-ldl编译选项

执行 

./hello
运行结果

InitEffect-->21
ReinitEffect-->24
Process-->28
SetParam-->31
GetParam-->34
Deallocate-->37
GetSpecBufAddress-->40
getValue-->7,param=getValuexxxxxxx,intparam=1
getValue1-->12,param=getValue1,intparam=1,isReturnAddress=0
main function return from getVaule1 =0







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值