Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Windows Callbacks

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