필터 지우기
필터 지우기

what data type do i need to calllib with pointer argument char*

조회 수: 10 (최근 30일)
Yoav Romach
Yoav Romach 2014년 5월 28일
댓글: Yoav Romach 2014년 5월 28일
Hey there! I'm using a shared library and I'm tring to call this function from it (from h file):
long PI_FUNC_DECL PI_EnumerateUSB(char* szBuffer, long iBufferSize, const char* szFilter);
So I'm making the following code:
>> szBuffer = libpointer('char');
>> iBufferSize = libpointer('int32');
>> calllib(libalias,'PI_EnumerateUSB',szBuffer,iBufferSize,'E-861');
Error using calllib
Pointer type must match data type
After some checking, the problem is without doubt the char* (szBuffer). From what I understand from Matlab Help I need it to be a char array, so what am I'm doing wrong?
p.s.
I have the problem in other functions as well that require char*
Thanks!

답변 (1개)

Philip Borghesani
Philip Borghesani 2014년 5월 28일
You are over thinking this let MATLAB do the work no libpointers are needed:
[status, resultString]=calllib(libalias,'PI_EnumerateUSB',blanks(100),100,'E-861')
The code you wrote is doing the equivalent of the c code:
char *szBuffer=NULL; int* iBufferSize=NULL;
PI_EnumerateUSB(szBuffer,*iBufferSize,"E-861");
and if you were using C your program would crash...
  댓글 수: 2
Yoav Romach
Yoav Romach 2014년 5월 28일
Hey Philip,
The problem is that the function changes the value of szBuffer, and that is what I need...
I actually solved my initial problem by writing:
szBuffer = libpointer('string',blanks(128));
szBuffer.value
calllib(libalias,'PI_EnumerateUSB',szBuffer,128,'')
szBuffer.value
The problem now is that szBuffer does not change its value :/
I also tried passing stringPtr, same results.
An equivalent Python code did work, and the function return value indicates that it ran without errors, so the function in the dll is working, but for some reason the value of szBuffer doesn't change... Any ideas?
Yoav Romach
Yoav Romach 2014년 5월 28일
ERR!
I Solved it, It's really simple but I couldn't find a single place in which this behavior is documented...
So in matlab, instead of changing szBuffer itself, it simply goes into the return value.
So instead of returning just [long] it now returns [long, string] and the string is the new szBuffer. Which means I really don't need pointers...
Why isn't it clearly written in the documentation... :(

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by