Have a C shared library call a Matlab callback function
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello, I have a C shared library that I load in my Matlab script. One of the functions of that shared library allows to register a callback function. The shared library will then call-back the registered function if function handleCallback is called. My problem is, I don't know how to pass the Matlab function to my shared library. See function registerCallback in my code below:
Following is the shared library interface object:
classdef myInterfaceObject
properties
libName;
end
methods
%constructor
function obj = myInterfaceObject(libname)
obj.libName = libname;
if ~libisloaded(obj.libName)
loadlibrary(obj.libName,@mySharedLibraryProto);
end
end
%destructor
function delete(obj)
unloadlibrary(obj.libName);
end
%the callback function we want to be called by the c shared library
function myMatlabCallback(obj,data)
disp('Inside myMatlabCallback');
end
%function to register the callback function above
function registerCallback(obj)
fPtr = libpointer('voidPtr',@obj.masterCb);
calllib(obj.libName,'c_registerCallbackFunction',fPtr);
end
%function that will trigger the callback call
function handleCallback(obj)
calllib(obj.libName,'c_handleCallback');
end
end
end
Following is my proto file for the shared library:
function [methodinfo,structs,enuminfo,ThunkLibName]=mySharedLibraryProto
ival={cell(1,0)};
structs=[];
enuminfo=[];
fcnNum=1;
fcns=struct('name',ival,'calltype',ival,'LHS',ival,'RHS',ival,'alias',ival);
ThunkLibName=[];
fcns.name{fcnNum}='c_registerCallbackFunction';
fcns.calltype{fcnNum}='cdecl';
fcns.LHS{fcnNum}=[];
fcns.RHS{fcnNum}={'voidPtr'};
fcnNum=fcnNum+1;
fcns.name{fcnNum}='c_handleCallback';
fcns.calltype{fcnNum}='cdecl';
fcns.LHS{fcnNum}=[];
fcns.RHS{fcnNum}=[];
fcnNum=fcnNum+1;
methodinfo=fcns;
And following is my test script:
function test()
objectInstance=myInterfaceObject('mySharedLibrary');
objectInstance.registerCallback();
objectInstance.handleCallback();
objectInstance.delete();
end
Thanks for any help!
0 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!