Attempt to call external dll function causing segmentation fault

조회 수: 2 (최근 30일)
Alex
Alex 2011년 4월 25일
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
Chirag Gupta
Chirag Gupta 2011년 4월 25일
could you also provide the signature that is shown by MATLAB when you type libfunctionsview USBm

댓글을 달려면 로그인하십시오.

채택된 답변

Chirag Gupta
Chirag Gupta 2011년 4월 25일
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
Alex
Alex 2011년 4월 26일
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 2011년 4월 26일
Not sure how this got accepted as the answer since I haven't solved this problem yet.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Malcolm Lidierth
Malcolm Lidierth 2011년 4월 27일
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 2011년 4월 26일
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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by