Attempt to call external dll function causing segmentation fault

5 views (last 30 days)
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
Chirag Gupta on 25 Apr 2011
could you also provide the signature that is shown by MATLAB when you type libfunctionsview USBm

Sign in to comment.

Accepted Answer

Chirag Gupta
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
Alex
Alex on 26 Apr 2011
I tried a buffer size up to 100000 with USBm_About and I tried using USBm_Copyright with a buffer size of 5K but still getting the same result unfortunately.
Alex
Alex on 26 Apr 2011
Not sure how this got accepted as the answer since I haven't solved this problem yet.

Sign in to comment.

More Answers (2)

Malcolm Lidierth
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.

Chirag Gupta
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

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!