Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a FB Sub from a DLL
#1
Is possible to call a freebasic procedure or function from a DLL?; Anyone has some example?.

Thks in Adv.
Tio Bit
Reply
#2
see exaples\dll\ in your freebasic directory
url]http://fbide.sourceforge.net/[/url]
Reply
#3
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)
Reply
#4
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.
Reply
#5
Ok, Thanks, it was very easy (adapted from the dll and export examples ):

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)