This is something I hacked together in a few hours. It has some tiny bugs and is incomplete but I just don't have more time to spend on it so it'll probably be alway a WIP. But it's useable none-the-less.
http://members.lycos.co.uk/sotmailings/stuff/funcref.htm
Download the zip file, it contains
TPC, Keyword file, example "callback DLL" and a DBP example file included that looks like this:
rem Initialisation ----------------------------------------------------------------
rem First we need to call each function to get their addresses:
Number1(1)
Number2(2)
Number3(3)
Number4(4)
SomethingToReturn()
CallBack(Val1)
randomize timer()
for i = 1 to 10
f = rnd(3)
inc f
rem Call the function by reference
call function f
next i
d$ = Call Function(5)
print d$
wait key
load dll "callback.dll",1
CallBack_Adr = FuncRef Address(6)
call dll 1,"UseCallBack",CallBack_Adr,12,42
wait key
function Number1(x)
rem This creates a function reference Alway place this in the first line!
rem Do not use local variables as parameters.
Make Funcref 1,1
rem "FUNCREF INIT" returns one if this is the first time a function reference is created
rem otherwise zero. Thiis way we can avoid a function call through
if funcref init(1) then exitfunction
print "I am Number 1"
endfunction
function Number2(x)
Make Funcref 2,1
if funcref init(2) then exitfunction
print "I am Number 2"
endfunction
function Number3(x)
Make Funcref 3,1
if funcref init(3) then exitfunction
print "I am Number 3"
endfunction
function Number4(x)
Make Funcref 4,1
if funcref init(4) then exitfunction
print "I am Number 4"
endfunction
function SomethingToReturn()
Make Funcref 5,0
if funcref init(5) then exitfunction ""
endfunction "Hello, World"
function CallBack(Val1)
Make Funcref 6,2
if funcref init(6) then exitfunction
print "I'm a CallBack and I've been Called!"
print "Val1: " + str$(Val1)
print "Val2: " + str$(Val2)
endfunction
Note: For certain reasons you can't do this:
for i = 1 to 4
call dll i
next i
instead do it like this
for i = 1 to 4
a = i
call dll a
next i