How to return an array of structure from a dll function?
5 views (last 30 days)
Show older comments
I am trying to get structure array output from a function in dll. A structure named ascmsg_struct is defined in the header file getasc.h as follows:
struct ascmsg_struct{
double raw_time;
unsigned int channel;
char msg_id[9];
char msg_data[17];
};
#define NO_OF_MSG 2000
A function named readAsc is defined in the readAsc.c file as follows:
int readAsc(struct ascmsg_struct* myArray)
{
int msg_idx=0;
FILE *file;
char line[256];
//Open file
file = fopen("TestFile.asc", "r");
if (!file)
{
fprintf(stderr, "Unable to open file.");
return EXIT_FAILURE;
}
while (fgets(line, sizeof(line), file) && msg_idx<NO_OF_MSG)
{
if(strstr(line, "Rx ") != NULL)
{
myArray[msg_idx]=process_line(line);
msg_idx++;
}
}
fclose(file);
return EXIT_SUCCESS;
}
Also function process_line is defined as follows:
struct ascmsg_struct process_line(char line[256])
{
struct ascmsg_struct outstruct= {0, 0, "", ""};
// After some processing prepare outstruct
outstruct.raw_time = 0.0;
outstruct.channel = 0;
strncpy(outstruct.msg_id,"12345678",8);
strncpy(outstruct.msg_data,"1122334455667788",16);
return outstruct;
}
A global variable holding array of structure is defined as follows:
struct ascmsg_struct myArray[NO_OF_MSG];
In the main function it is used as follows:
struct ascmsg_struct* ptr=NULL;
ptr=&myArray[0];
and later...
if(readAsc(ptr)==EXIT_SUCCESS)
printf("Asc read = SUCCESS\n");
After doing this I have my intended structure array in myArray[NO_OF_MSG].
I have generated dll and I would like to output myArray[NO_OF_MSG] to matlab. How do I do that?
People are saying matlab does not handle structure from dll very well ans some are saying it is supported. Please see this .
After generating dll, I am trying this:
[notfound,warnings]=loadlibrary('readasc.dll','readasc.h');
sp=libpointer('ascmsg_structPtr');
calllib('libreadasc', 'readFullAsc',sp); <--This causes matlab to crash. I believe some pointer issue.
What am I doing wrong? Any help is greatly appreciated.
Many Thanks,
Nambaliya
0 Comments
Answers (0)
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!