How do I manipulate multilayer pointers in calllib calls, what is the proper syntax?

7 次查看(过去 30 天)
I have access to a library with the following calls:
[int32, BVTOpaqueSonarPtr, BVTOpaqueHeadPtrPtr] = BVTSonar_GetHead (BVTOpaqueSonarPtr, int32, BVTOpaqueHeadPtrPtr)
and
[int32, BVTOpaqueHeadPtr, BVTOpaquePingPtrPtr] = BVTHead_GetPing (BVTOpaqueHeadPtr, int32, BVTOpaquePingPtrPtr)
In my MATLAB function I have the following:
sonarPtr = libpointer;
headPtrPtr = libpointer;
[retval, sonarPtr, headPtrPtr] = BVTSonar_GetHead(sonarPtr, headIndex, headPtrPtr);
Now it is not clear to me how to dereference headPtrPtr to make the call to get the ping. It will look something like:
pingPtrPtr = libpointer;
[retval, *headPtr, pingPtrPtr] = BVTHead_GetPing(*headPtr, pingNumber, pingPtrPtr);
Except the '*' obviously does not work and I cannot find the proper syntax. I will have a similar problem using the BVTPing commands to get information from the ping, since they all call for pingPtr.
eg. [int32, BVTOpaquePingPtr, BVTOpaqueMagImagePtrPtr] BVTPing_GetImageRTheta (BVTOpaquePingPtr, BVTOpaqueMagImagePtrPtr)

采纳的回答

Philip Borghesani
Philip Borghesani 2013-7-29
编辑:Philip Borghesani 2013-7-29
MATLAB automatically does the pointer conversions for you there is no need to add a star.
You did not post the symptoms or specifics of your problem so you get some general comments.
  • libpointer objects work like handle objects. They can and will be modified when passed into a function on the right hand side of a calllib call there is no need to assign back into them on the left hand side of the calllib command. Create a new variable if needed to accept the output. Placing the same variable on both sides should work but places extra stress on the system because temporaries are needed to hold the input before the variable can be destroyed and the new copy (the input) can be copied into it.
  • Passing an empty libpointer object will pass a NULL pointer to the function being called. A null may be correct for head in getHead but is most likely wrong for the sonar value. Your code should probably look something like this:
sonarStruct=libstruct('BVTOpaqueSonar',matlabStructWithInputValues);
headPtr=libPointer('BVTOpaqueHead') % null typed pointer to hold head might need BVTOpaqueHeadPtr instead
retval=calllib('libname','BVTSonar_GetHead',sonarStruct, headPtr);
After the call sonarStruct and headPtr will contain modified values if modified by the function call.
  2 个评论
Gwyneth
Gwyneth 2013-7-30
Hello, thank you for your input. The error I encountered was a return value from the library indicating "incorrect arguments".
I neglected to display for you the assignment of the sonarPtr value, these lines of code preceded the call to attach the sonar head:
sonarPtr = calllib(SDK_Library_Name, 'BVTSonar_Create');
retval = calllib(SDK_Library_Name, 'BVTSonar_Open', sonarPtr, 'FILE', SonFileName);
So sonarPtr would not have been null anymore when it was sent to BVTSonar_GetHead. The headPtrPtr would have been null still as it would then get assigned in the library call.
I have modified the code to the following and still get a return value for incorrect argument. I will continue to try to work out the error.
headPtrPtr = libpointer('BVTOpaqueHeadPtrPtr');
headnum = headIndex;
retval = calllib(SDK_Library_Name,'BVTSonar_GetHead',sonarPtr, headnum, headPtrPtr);
Meanwhile, I am still confused on the basic premise. When I do have two function calls: retval = librarycall1(BVTHead *head); retval = librarycall2(BVTHead head);, the variable I declare in MATLAB will go into the librarycall1 and be returned as a pointer to a BVTHead opaque object. Then to send that variable into librarycall2, I do not have to dereference it in any way?
Thank you for your guidance.
Philip Borghesani
Philip Borghesani 2013-7-30
编辑:Philip Borghesani 2013-7-30
MATLAB checks the number of levels of indirection needed on input parameters to a calllib command and adds any that are needed. There is no difference in MATLAB between a libpointer('myPtr') and libpointer('myPtrPtr') (except when using strings or cstrings).
For headPtr you probably want to use:
headPtr=libpointer('BVTOpaqueHeadPtr') % or libpointer('voidPtr')
What version of MATLAB are you using? There are bugs in version prior to R2010a that could cause issues.
When I Googled for 'BVTSonar_GetHead' I found and example.m file that does the initial setup and some use of this dll it may be a good starting point for you.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call C from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by