uses "console"
'----------------------------------------------------------------------------
' This example shows how to use FreeBASIC compiled functions from
' thinBasic script without the need to DECLARE FreeBASIC functions as
' expternal DLL functions.
' thinBasic will take care of it
'----------------------------------------------------------------------------
#Compiled
'----------------------------------------------------------------------------
' Code inside #Compiled ... #EndCompiled will be compiled on the fly using FreeBASIC
' compiler into a DLL whose name is the same name of the script
'----------------------------------------------------------------------------
'----------------------------------------------------------------------------
' There can be multiple #Compiled ... #EndCompiled blocks
' thinBasic will take care to concatenate into a single FreeBASIC source code
' before compiling
'----------------------------------------------------------------------------
#EndCompiled
#Compiled "===Scalar numeric variables==="
'----------------------------------------------------------------------------
' ATTENTION:
' FreeBASIC Functions declared with "CDecl" and "Export" will be visible
' to thinBASIC code. Parameters and return value must be compatible with
' thinBASIC variable types
'----------------------------------------------------------------------------
'----------------------------------------------------------------------------
function FB_Check_LongByVal Cdecl (ByVal lLong1 as Long, ByVal lLong2 as Long) As Long Export
'----------------------------------------------------------------------------
print "Long 1 from inside FB passed ByVal", lLong1
lLong1 = lLong1 * 2
return lLong1
end Function
'----------------------------------------------------------------------------
function FB_Check_LongByRef Cdecl (ByRef lLong1 as Long, ByVal lLong2 as Long) As Long Export
'----------------------------------------------------------------------------
print "Long 1 from inside FB passed ByRef", lLong1
lLong1 = lLong1 * 2
return lLong1
end Function
'----------------------------------------------------------------------------
function FB_Check_DoubleByVal Cdecl (ByVal lDouble1 as Double, ByVal lDouble2 as Double) As Double Export
'----------------------------------------------------------------------------
print "Double 1 from inside FB passed ByVal", lDouble1
lDouble1 = lDouble1 * 2.5
return lDouble1
end Function
'----------------------------------------------------------------------------
function FB_Check_DoubleByRef Cdecl (ByRef lDouble1 as Double, ByVal lDouble2 as Double) As Double Export
'----------------------------------------------------------------------------
print "Double 1 from inside FB passed ByRef", lDouble1
lDouble1 = lDouble1 * 2.5
return lDouble1
end Function
#EndCompiled
#Compiled "===Numeric arrays==="
'============================================================================
' Numeric ARRAYs
'----------------------------------------------------------------------------
' the way numeric arrays are handled by FreeBASIC and ThinBASIC is very different but
' they way memory is allocated is the same: numeric array data is just a consecutive
' sequence of bytes
' So passing a numeric array from ThinBASIC to a FreeBASIC function is easy as
' passing BYREF the first element of the numeric array and than inside FreeBASIC
' use that first element memory pointer with some FreeBASIC pointers math
' More info on FreeBASIC pointers: http://www.petesqbsite.com/sections/express/issue18/fb_pointers.html
'----------------------------------------------------------------------------
'----------------------------------------------------------------------------
function FB_Check_LongArray Cdecl (byref pLong as long, byval lMaxElements as long) As long Export
' passing a LONG ByRef means passing a pointer to the memory location where the first long is allocated
'----------------------------------------------------------------------------
Dim pArray As long Ptr
dim lIdx as Long
'---Get a pointer to the first element
pArray = @pLong
print "Ptr to an Arrays of LONG", pArray
print "Maximum elements", lMaxElements
print "Values are:"
for lIdx = 1 to lMaxElements
'---Here we access array elements values using pinter aritmentic
'---*(p + lIdx - 1) means access the first element offset lIdx - 1
'---So ... element 1 equal *(pArray + 1 - 1)
' element 2 equal *(pArray + 2 - 1)
' element x equal *(pArray + x - 1)
print chr(9), "Position", lIdx, "Value", *(pArray + lIdx - 1),
'---Multiply by 2 and add 5 to the array element
*(pArray + lIdx - 1) = *(pArray + lIdx - 1) * 2 + 5
'---Show new value
print "New value", *(pArray + lIdx - 1)
next
return 0
end Function
#EndCompiled
dim MyScriptLong1 as long
dim MyScriptLong2 as long
dim MyScriptDouble1 as double
dim MyScriptDouble2 as Double
'---LONG BYVAL
MyScriptLong1 = 123
printl "---Calling a FB function passing a LONG ByVal and returning a LONG---" in %CCOLOR_FYELLOW
printl "Long 1 from thinBasic script BEFORE calling FB function", MyScriptLong1
'---Call a compiled FreeBASIC function
printl "FB function return value", FB_Check_LongByVal(MyScriptLong1, MyScriptLong2)
printl "Long 1 from thinBasic script AFTER calling FB function", MyScriptLong1
PrintL "---------------------------------------------------------------------"
PrintL
'---LONG BYREF
MyScriptLong1 = -123
printl "---Calling a FB function passing a LONG ByRef and returning a LONG---" in %CCOLOR_FYELLOW
printl "Long 1 from thinBasic script BEFORE calling FB function", MyScriptLong1
'---Call a compiled FreeBASIC function
printl "FB function return value", FB_Check_LongByRef(MyScriptLong1, MyScriptLong2)
printl "Long 1 from thinBasic script AFTER calling FB function", MyScriptLong1
PrintL "---------------------------------------------------------------------"
PrintL
'---Double BYVAL
MyScriptDouble1 = 123.456
printl "---Calling a FB function passing a Double ByVal and returning a Double---" in %CCOLOR_FYELLOW
printl "Double 1 from thinBasic script BEFORE calling FB function", MyScriptDouble1
'---Call a compiled FreeBASIC function
printl "FB function return value", FB_Check_DoubleByVal(MyScriptDouble1, MyScriptDouble2)
printl "Double 1 from thinBasic script AFTER calling FB function", MyScriptDouble1
PrintL "---------------------------------------------------------------------"
PrintL
'---Double BYREF
MyScriptDouble1 = -123.456
printl "---Calling a FB function passing a Double ByRef and returning a Double---" in %CCOLOR_FYELLOW
printl "Double 1 from thinBasic script BEFORE calling FB function", MyScriptDouble1
'---Call a compiled FreeBASIC function
printl "FB function return value", FB_Check_DoubleByRef(MyScriptDouble1, MyScriptDouble2)
printl "Double 1 from thinBasic script AFTER calling FB function", MyScriptDouble1
PrintL "---------------------------------------------------------------------"
PrintL
'---ARRAY of LONG
'[breakpoint] Arrays of LONG passed from TB to FB and changed inside FB
dim ArrayOfLongs(10) as Long = 10,20,30,40,50,60,70,80,90,100
printl "---Calling a FB function passing BYREF the first element of an array of longs---" in %CCOLOR_FYELLOW
FB_Check_LongArray(ArrayOfLongs(1), 10)
PrintL "---------------------------------------------------------------------"
PrintL
printl "---Press a key to end---"
WaitKey
Bookmarks