现在的位置: 首页 > 综合 > 正文

dynamic loading

2013年09月10日 ⁄ 综合 ⁄ 共 1347字 ⁄ 字号 评论关闭
 /*******************************************************************
 * main.cpp
 * Created 2006 by hjm.
 ********************************************************************/    
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

#define ASSERT(expect,result,function) /
if(expect==result)/
{printf("The function %s is OK!/n",function);}/
else/
{printf("The function %s is WRONT!/n",function);}

int main(int argc,char **argv)
{
    void *libHandle=NULL;
    char *errmsg;
    int (*ApiVideoEnableVideo)(int enable);
    int (*ApiVideoIsVideoEnable)(void);

    //open lib

    libHandle=dlopen("./lib/libipcam_api.so",RTLD_NOW);
    if(libHandle==NULL)
    {
        fprintf(stderr,"Failed to load libipcam_api.so:%s/n",dlerror());
        return -1;
    }
    dlerror();//clear errors
    
    ApiVideoEnableVideo=dlsym(libHandle,"ApiVideoEnableVideo");//load ApiVideoEnableVideo
    if((errmsg=dlerror())!=NULL)
    {
        fprintf(stderr,"Didn't find ApiVideoEnableVideo:%s/n",errmsg);
    }
    
    ApiVideoIsVideoEnable=dlsym(libHandle,"ApiVideoIsVideoEnable");//load ApiVideoEnableVideo
    if((errmsg=dlerror())!=NULL)
    {
        fprintf(stderr,"Didn't find ApiVideoEnableVideo:%s/n",errmsg);
    }
    
    //call the loaded function
    int a=0;
    ApiVideoEnableVideo(1);
    ASSERT(1,ApiVideoIsVideoEnable(),"ApiVideoIsVideoEnable")
    
    a=ApiVideoIsVideoEnable();
    printf("a=%d/n",a);
    dlclose(libHandle);
    printf("sdsd/n");
    return 0;
}

抱歉!评论已关闭.