Using functions from C++ in MATLAB

10 views (last 30 days)
Keegan Chafin
Keegan Chafin on 12 Jun 2020
Answered: Anjaneyulu Bairi on 1 Nov 2024 at 16:14
Hi all,
I'll start off by showing a function that I have in C++.
int __stdcall UDP_GigE_getData(unsigned char *packetType, T_GVSP_LEADER_PACKET *leader, T_GVSP_PAYLOAD_PACKET *payloads,
T_GVSP_TRAILER_PACKET *trailer, T_GVSP_ALL_IN_PACKET *allInOne)
You should know that all variables with type T_GVSP_... are struct types defined in C. Now, I want to use the calllib function in MATLAB to call the above function. Something like,
response = calllib( 'UDP_GigE' , 'UDP_GigE_getData' , packetType, leader, payloads, trailer, allInOne)
The problem is, I do not know how put those struct pointers in the calllib function, since MATLAB's pointer functionality is not to obvious to me. Does anyone have any ideas on how to put pointers into the calllib function correctly?
Thanks

Answers (1)

Anjaneyulu Bairi
Anjaneyulu Bairi on 1 Nov 2024 at 16:14
Hi,
To call a C++ function from MATLAB using "calllib", you need to ensure that the data types in MATLAB match those expected by the C++ function. In your case, you need to handle pointers to structures, which requires some setup in MATLAB.
  • Define the structures in MATLAB using "libstruct"
  • Create pointer to those structures using "libpointer"
  • Now call the function with created pointers like
calllib('UDP_GigE' , 'UDP_GigE_getData' , packetType, leaderptr, payloadsptr, trailerptr, allInOneptr)
You can refer the below documentation links to know more information on "libstruct","libpointer" and for few examples:

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!