Attempt to call external dll function causing segmentation fault
5 views (last 30 days)
Show older comments
I'm trying to use a third-party external DLL (from usbmicro) within matlab, but it keeps crashing matlab. This is from the documentation indicating the syntax of the function call from within a C program:
int USBm_About( char *about );
I tried this matlab script (yes it's very kludgy, I'm a matlab noob):
>> loadlibrary('USBm.dll','USBmAPI.h')
>> libfunctions('USBm')
>> s='sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss';
>> st=strcat(s,s,s,s);
>> vp = libpointer('voidPtr',[int8(st) 0]);
>> result=calllib('USBm','USBm_About',vp)
and this one:
>> loadlibrary('USBm.dll','USBmAPI.h')
>> libfunctions('USBm')
>> s='sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss';
>> st=strcat(s,s,s,s);
>> vp=libpointer('cstring',st);
>> result=calllib('USBm','USBm_About',vp)
In both cases, the calllib() call causes matlab to crash with a segmentation fault.
The version of matlab is 7.10; the OS is Windows Vista.
Update: Here's a screenshot of the "libfunctionsview USBm"
1 Comment
Chirag Gupta
on 25 Apr 2011
could you also provide the signature that is shown by MATLAB when you type libfunctionsview USBm
Accepted Answer
Chirag Gupta
on 25 Apr 2011
Actually, it might show up as a cstring. In that case, basically its char* about is a return type and all you need to pass it is a empty buffer.
b = char(zeros(1,1024)); % Create a 1024 char empty buffer
[a,b] = calllib('USBm','USBm_About',b)
5 Comments
More Answers (2)
Malcolm Lidierth
on 27 Apr 2011
Don't use b = char(zeros(1,1024)); That's a zero length string (terminated in C by the first zero element) which may or may not get allocated by MATLAB's lazy memory management and could get deallocated at any time (e.g. when its referenced in MATLAB on the RHS in calllib).
For char, pre-allocate with ones(...) instead. It sounds like you should use uint8 anyway, 1-byte per element instead of 2 for char (16-bit unicode). Zeros(...) should be OK then.
0 Comments
Chirag Gupta
on 26 Apr 2011
Regarding who accepted the answer, I have no idea. I was able to download the dll from the usbmicro website, but was unable to load it into my 64bit windows machine.
The errors thrown were related to the fact that the DLL was using Strings [C++ types].
That would explain the reason for the errors. I would still assume that you should be able to use the remaining functionality of the DLL
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!