How to load a freebasic DLL with o2

Started by Frank Brübach, June 22, 2024, 02:11:53 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

Hello Charles..

Do you have any examples how to load an external freebasic DLL file with oxygen?

Freebasic


' file: batman32.bas
' compile as: fbc -dll batman32.bas
#include once "Batman32.bi"

function HelloBatman(ByVal s As string) as String Export
    return "HelloBatman "+s
End function

This compiling with freebasic produces a batman32.dll File and all OK Here

But how to load the dll to oxygen with using the function ?

Batman32.bi File
' batman32.bi
declare function HelloBatman Alias "HelloBatman"(ByVal s As String) as String

Last Not least Test File
Freebasic
' test-batman32.bas file to test Batman32.dll
'
#include "batman32.bi"
#inclib "batman32"

Dim s As String
s="World!"

print HelloBatman(s) ' result HelloBatman World

Print "ok"
sleep

Charles Pegge

#1
Hi Frank,

I have not used Freebasic since 2017 so I refer to my experiebce with eatlier O2 compilations.

First make sure your FreeBasic DLL declarations do not produce C-mangled names

Second, FreeBasic strings are not the same as OxygenBasic strings so you need to use a compatible type: zstring ptr / char ptr

' batman32.bi
  #ifdef OS_MS
    extern "Windows-MS"
  #else
    extern "C"
  #endif
' file: batman32.bas
' compile as: fbc -dll batman32.bas

'#include once "Batman32.bi"
declare function HelloBatman Alias "HelloBatman"(ByVal s As zString ptr) as zString ptr

function HelloBatman(ByVal s As zstring ptr) as zstring ptr Export
    return "HelloBatman "+s
End function

end extern

OxygenBasic client

extern lib "batman32.dll"
declare function HelloBatman (char*) as char*
end extern

char s[]="World!"

print HelloBatman(s) ' result HelloBatman World

Print "ok"