02-27-2005, 09:48 PM
02-27-2005, 10:15 PM
see exaples\dll\ in your freebasic directory
02-27-2005, 10:26 PM
Hi VonGodric; But in that example you call a function defined in the DLL from the EXE file (the normal way); I want to know if it's possible to call a Function defined on the main EXE from the DLL (something like a callback function)
02-27-2005, 10:37 PM
Do you mean using EXPORT on the sub in the main exe code and using GetProcAddress to get this function in the DLL code. Look at the export and import examples in the export folder.
02-27-2005, 11:08 PM
Ok, Thanks, it was very easy (adapted from the dll and export examples ):
TEST.BAS
and
MYDLL.BAS
mydll.bi doesn't change.
TEST.BAS
Code:
''
'' test -- calls a mydll's function and prints the result
''
'' compile as: fbc test.bas (couldn't be simplier, eh?)
''
'$include: 'mydll.bi'
print "1 + 2 ="; addnumbers( 1, 2 )
function MultiplyNumbers cdecl alias "MultiplyNumbers" ( byval operand1 as integer, byval operand2 as integer ) as integer export
print "Called from the DLL"
MultiplyNumbers = operand1 * operand2
end function
and
MYDLL.BAS
Code:
.
.
.
Sub Multiply
'' create a function pointer, arguments must be the same as in the original function
dim MultiplyNumbers as function ( byval operand1 as integer, byval operand2 as integer ) as integer
'' get our handler
dim exehandle as integer
exehandle = GetModuleHandle( NULL )
'' find the proc address (case matters!)
MultiplyNumbers = GetProcAddress( exehandle, "MultiplyNumbers" )
'' call it..
print " 3 * 5 = "; MultiplyNumbers( 3 , 5 )
End Sub
''::::::
''
'' simple exported function, the full prototype is at mydll.bi
''
function AddNumbers ( byval operand1 as integer, byval operand2 as integer ) as integer
AddNumbers = operand1 + operand2
Multiply
end function
mydll.bi doesn't change.