This is a mirror of official site: http://jasper-net.blogspot.com/

Uncovering the Hidden DLL Function Callback Feature

| Monday, November 29, 2010
I took a little break today and dropped-in on the REBOL3 AltME world to find an interesting question being asked by Cyphre and a few other users: The word CALLBACK appears in the REBOL binary, but what does it do? Here is some little known information about this feature.

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

Posted via email from .NET Info

0 comments: