Callbacks in DLL Functions (Routines)
When using the External Library Interface (DLLs), you can pass a REBOL function to be called back from within a DLL function. REBOL will deal with the argument conversions in both directions, but you still have to write it with great care, because interfacing in this way to DLL code is tricky business.
Example of a Callback
Here is an example written by Cyphre that helps show the way a callback function works. In REBOL you would write a routine (a DLL interface function) such as:
test: make routine! [
a [int]
b [int]
c [callback [int int return: [int]]]
return: [int]
] test-lib "test"
Here the c argument is a callback function interface specification that takes two integers and returns an integer result. Note that the argument names are not provided, only their datatypes.
Then, in the test.dll code you might write the something like:
extern "C"
MYDLL_API int test(int a, int b, int (*pFunc)(int, int))
{
int result = pFunc(a, b);
return result;
}
And finally, try it out, you would write the actual callback function such as:
add-it: func [a b][return a + b]
Read more: Rebol