Windows Callbacks
[1/4] from: ric_shepard::fishgame::state::ak::us at: 2-Jan-2001 7:01
Anyone know how to create callback functions under Windows
using REBOL/Command? My needs are pretty minimal but I
do need to process 'Windows Messages'. TIA.
--
Ric Shepard <[ric_shepard--fishgame--state--ak--us]>
Microcomputer/Network Specialist
Alaska Department of Fish and Game
[2/4] from: jeff:rebol at: 2-Jan-2001 10:14
Howdy, Ric:
You can't really make a callback function in REBOL, meaning
like you have a REBOL function and you have a library
routine take a pointer to that REBOL function as a
callback. At this time you really have to have C functions
act as callbacks. You can manage this with REBOL/command
like this:
Create another dynamic library containing your callback
functions and a function that returns a pointer to those
functions. REBOL/command script gets those pointer to
functions as integers and passes them along into routines
that expect pointers to call back functions.
You could have a callback function modify globals that the
command script could check...
I can provide a more complete example if you like.
-jeff
[3/4] from: ric_shepard:fishgame:state:ak:us at: 2-Jan-2001 10:05
Thanks, Jeff ... and ... a more complete example would be
stupendous - - if you have the time.
-ric
[jeff--rebol--net] wrote:
> Howdy, Ric:
> You can't really make a callback function in REBOL, meaning
<<quoted lines omitted: 24>>
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
--
Ric Shepard <[ric_shepard--fishgame--state--ak--us]>
Microcomputer/Network Specialist
Alaska Department of Fish and Game
[4/4] from: jeff:rebol at: 3-Jan-2001 9:02
> Thanks, Jeff ... and ... a more complete example would be
> stupendous - - if you have the time.
>
> -ric
Okay, let's try:
A callback function might modify a global that REBOL has.
Consider the following C code:
/*****************************************/
int global = 0;
typedef void (*p_func)();
void callback () { global++; }
int * get_global () { return &global; }
p_func get_callback () { return callback; }
/*****************************************/
So we put the above code in a shared library, (callback.so, or
callback.dll). Okay, now some REBOL/command code:
;;------------------------------------------
REBOL [
Title: "Callback Command script"
Neeeds: [command]
]
cb-lib: load/library %callback.so
get-callback: make routine! [return: [integer!]] cb-lib "get_callback"
get-global: make routine! [return: [struct [global [int]]]] cb-lib "get_global"
struct-global: get-global
callback: get-callback
do-something-with-callback callback
start-global: struct-global/global
forever [
if start-global <> struct-global/global [
print "something's changed global"
]
]
halt
;;------------------------------------------
Why did we return global in a struct? Because in
REBOL/Command, structs are always passed by reference, and a
pointer to a stuct is a pointer to it's first element. This
allows us to get the necessary indirection to our global.
The callback will modify global, and REBOL/Command can see
global being modified because it has a reference to global
inside struct-global.
This all assumes that you've got the callback happening in a
separate thread with shared memory or something, while your
command script is still running. I don't know if this is
the case. If not, this whole example may have been silly,
but I hope it was useful for something. :-)
-jeff
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted