c++實(shí)現(xiàn)加載so動(dòng)態(tài)庫(kù)中的資源
更新時(shí)間:2016年12月31日 10:12:43 投稿:jingxian
下面小編就為大家?guī)?lái)一篇c++實(shí)現(xiàn)加載so動(dòng)態(tài)庫(kù)中的資源。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
實(shí)例如下:
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <iostream>
//編譯命令 g++ -m32 EncodeOrDecode.cpp -ldl -o edcode
//名字通過(guò)nm -D xxxxxx.so獲取
using namespace std;
int main(int argc,char **argv){
void *handle = dlopen("./libpub_com_service.so",RTLD_LAZY);
if(!handle){
printf("open libpub error ,dlerror=%s\n",dlerror());
return -1;
}
string src = "05d7151349b7fe9b54306d90f9c938b3";
string desKey = "pub.%?d5";//58
printf("start ..src=[%s].....size=[%d].\n",src.c_str(),src.size());
//encode
typedef string (*encode_t)(const string encodeSrc,const string encodeKey);
encode_t encode = (encode_t)dlsym(handle,"_ZN10NS_PUB_API9DesEncodeESsSs");
if(!encode){
printf("not found encode function ,dlerror= %s \n",dlerror());
dlclose(handle);
return -1;
}
string encodeStr = encode(src,desKey);
printf("get the encode function success ..encodeStr=[%s].....size=[%d]\n",encodeStr.c_str(),encodeStr.size());
//decode
typedef bool (*decode_t)(const string decodeSrc,const string decodeKey,string &outResult);
decode_t decode = (decode_t)dlsym(handle,"_ZN10NS_PUB_API9DesDecodeESsSsRSs");
if(!decode){
printf("not found decode function ,dlerror= %s \n",dlerror());
dlclose(handle);
return -1;
}
string decodeOut;
bool ret = decode(encodeStr,desKey,decodeOut);
printf("get the decode function success ...ret=[%d]...decodeOut=[%s].....size=[%d]\n",ret,decodeOut.c_str(),decodeOut.size());
//end
dlclose(handle);
return 0;
}
以上就是小編為大家?guī)?lái)的c++實(shí)現(xiàn)加載so動(dòng)態(tài)庫(kù)中的資源全部?jī)?nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
C語(yǔ)言中動(dòng)態(tài)內(nèi)存分配malloc、calloc和realloc函數(shù)解析
C語(yǔ)言跟內(nèi)存申請(qǐng)相關(guān)的函數(shù)主要有 alloca、calloc、malloc、free、realloc等,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中動(dòng)態(tài)內(nèi)存分配malloc、calloc和realloc函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-03-03
Visual?Studio?2022?安裝低版本?.Net?Framework的圖文教程
這篇文章主要介紹了Visual?Studio?2022?如何安裝低版本的?.Net?Framework,首先打開(kāi)?Visual?Studio?Installer?可以看到vs2022?只支持安裝4.6及以上的版本,那么該如何安裝4.6以下的版本,下面將詳細(xì)介紹,需要的朋友可以參考下2022-09-09
通過(guò)一個(gè)小例子來(lái)簡(jiǎn)單理解C語(yǔ)言中的內(nèi)存空間管理
這篇文章主要介紹了通過(guò)一個(gè)小例子來(lái)簡(jiǎn)單理解C語(yǔ)言中的內(nèi)存空間管理,涉及到堆和棧等數(shù)據(jù)結(jié)構(gòu)的基本知識(shí),需要的朋友可以參考下2015-11-11

