フィルターのクリア

Attempt to call external dll function causing segmentation fault

6 ビュー (過去 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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by