I have a function named testdynamic which is called dynamically with dlopen and dlsym. Now, I have created a structure:
typedef struct BigStruct{
char el[2000];
}BigStruct;
which is used to store the parameters for the function. Then, I allocate space to a variable named:
void *cur = (void*)malloc(totalSize);
where, totalSize is the size of the parameters. I have this information beforehand.
After that I copy all the parameters to cur.
Then, I cast it to BigStruct like this:
BigStruct *bg;
bg = (BigStruct*)cur;
And run it like this:
void* ch = (void*)testdynamic(*bg);
Now in the function testdynamic when I am printing the parameters, I am getting correct values for all data types like char**
, int*
, int, etc.
The only data type which is not working is char*. Even before calling the function with *bg
, the contents of bg->el is correct even for char*. But, after calling, an error occurs.
What could be the problem?
Here is the code of testdynamic
char* testdynamic(char* x, char* y){
printf("%s %s\n", x, y);
return "hello";
}
I want to pass the parameters to the function testdynamic from my code.
This testdynamic can be any function which could accept any parameter of any type.
I get the information about the function during runtime. Since the size of char* is 1, I am casting everything to char* and then passing it to the function.
At this point, I am getting a runtime error if I am printing anything inside testdynamic which is of type char*
.