How to convert a function handle to a integer
6 views (last 30 days)
Show older comments
Hi, I need to call a Delphi function, that requires a function handle converted to an Integer. Here is the original Delphi dode:
Instantiate(cParameter, Integer(@MyCallback)) , where cParameter is some double.
I would call the Instantiate function via a library call
calllib('myDelphiLib','Instatiate',cParameter, ? )
What to fill in for the question mark? How do I pass a function handle that needs to converted to an Integer in Matlab?
Thanks for your help
0 Comments
Answers (2)
Walter Roberson
on 3 Mar 2017
When I look at your code, I get the impression that MyCallback will be a Delphi-Coded routine, rather than a MATLAB function handle. If so, then is there a call you can make into Delphi to get it to return @MyCallback ?
If there is, then you can handle the situation in MATLAB using lib.pointer, perhaps created with the convenience function libpointer() .
If there it is a Delphi routine you need the pointer to and there is no call you can make from outside Delphi to get the pointer, then you could have difficulty. Depending how Delphi manages external names, there is the possibility that the name is private to the Delphi file it is declared in (I am not familiar with Delphi linkage conventions.)
If it is a MATLAB function handle whose address you need to pass in to Delphi, then that situation could make sense if you were setting up a situation where Delphi become responsible for calling back to MATLAB. I would, though, want to know more about the callback mechanism; for example if it were by using the MATLAB Engine API then this is not the way I would proceed.
If you are trying to pass in a MATLAB function handle to Delphi with the expectation that Delphi would be able to directly call the routine as if it were a native Delphi routine, then that is something that is not possible -- not if the handle is with an interactive MATLAB session or with an MATLAB Compiler or MATLAB Compiler SDK compiled routine. For something like this to have a chance you would need to be using MATLAB Coder.
As yet I find no official way to convert a function handle to a generic pointer. I know that you can pass a function handle to a mex routine; see https://www.mathworks.com/matlabcentral/answers/96775-how-do-i-pass-function-handles-to-c-mex-function-in-matlab-7-8-r2009a
0 Comments
Arnav Mendiratta
on 3 Mar 2017
As long as your function handle returns a valid value, you should be able to typecast it into any kind of integer.
For example, let's define your function to return a 'double' type like this:
MyCallback = @(n) n.^2;
Now, if you call MyCallback, and notice the workspace:
>> value = MyCallback(2)
value =
4
>> whos
Name Size Bytes Class Attributes
MyCallback 1x1 32 function_handle
value 1x1 8 double
Considering your example, you can now typecast this value in your function using any of the integer classes that MATLAB supports.
In this case, you would replace the 'question mark' in your code with a typecasted value returned by function handle:
calllib('myDelphiLib','Instatiate',cParameter, uint8(MyCallback(2)) )
1 Comment
Walter Roberson
on 3 Mar 2017
Edited: Walter Roberson
on 3 Mar 2017
What you have described is for changing the type of what is returned by invoking MyCallback(2), not for transfering the function handle itself.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!