transfer the data between shared library and application

泄露秘密 提交于 2019-12-12 03:35:21

问题


I have shared library where I does some data generations/processing , and I have written some APIs and application to access them and transfer the data .

/**************APPLICATION*********/

 char* data1;

 char* data2;

 genratedCamData(1, char* data1 , char *data2);

 printf(" data1 %s ",data1);
 printf(" data2 %s ",data2);
 free(data2);

/************Inside Library ****************/

 int genratedCamData(1, char* datafirst , char *datasecond)

 {

 if(CAM==1)
 datafirst=getCam1data();

 printf(" test at lib %s ",type);

 datasecond=malloc(sizeof(char) * 100);
 sprintf(datafirst,"%s",datasecond);

 return 0;

 }

I tried above method get the data to application , but data prints properly inside the library but out side the lib (in the application ) it does not print anything ...

Can some body help me to use a best way to communicate the data b/w libs and application .


回答1:


As C passes arguments by value, including pointers, you need to pass the address of the pointers, char**, to a function to enable the function to point the pointers to a different address and have those changes visible to the caller:

int genratedCamData(int CAM, char** datafirst , char **datasecond)
{
    *datafirst=getCam1data();
    *datasecond=malloc(100); /* sizeof(char) is guaranteed to be 1. */
}

Invoked:

char* data1;
char* data2;
genratedCamData(1, &data1 , &data2);


来源:https://stackoverflow.com/questions/14200595/transfer-the-data-between-shared-library-and-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!