Attempt to call external dll function causing segmentation fault

1 visualizzazione (ultimi 30 giorni)
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 Commento
Chirag Gupta
Chirag Gupta il 25 Apr 2011
could you also provide the signature that is shown by MATLAB when you type libfunctionsview USBm

Accedi per commentare.

Risposta accettata

Chirag Gupta
Chirag Gupta il 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 Commenti
Alex
Alex il 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 il 26 Apr 2011
Not sure how this got accepted as the answer since I haven't solved this problem yet.

Accedi per commentare.

Più risposte (2)

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

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by