How to call a DLL created with FreeBasic from FreeBasic

General FreeBASIC programming questions.
Post Reply
Tolo68
Posts: 105
Joined: Mar 30, 2020 18:18
Location: Spain

How to call a DLL created with FreeBasic from FreeBasic

Post by Tolo68 »

Hello everyone!!!

I have created a DLL with the following code that I saw on the internet, once compiled, it works with VB6 and VB Net 2010, but I don't know how to use it with FreeBasic.

-------------------------------------------------------------
#include once "windows.bi"
#include once "win/ole2.bi"

EXTERN "windows-ms"

FUNCTION AddNumbers ( _
BYVAL operand1 AS INTEGER, BYVAL operand2 AS INTEGER ) AS INTEGER EXPORT
RETURN operand1 * operand2
END FUNCTION

FUNCTION UpperCase(BYVAL arg AS ZSTRING PTR) AS BSTR EXPORT
DIM res AS BSTR, s AS STRING
s = UCASE(*arg)
res = SysAllocStringByteLen(STRPTR(s), LEN(s))
RETURN res
END FUNCTION

END EXTERN


-------------------------------------------------------------

To call it from VB6 or VB Net, I use the following code..........

Declare Function AddNumbers Lib "FB_Dll.dll" (ByVal operand1 As Integer, ByVal operand2 As Integer) As Integer
Print AddNumbers (21,37)

But if I use the above code in FreeBasic it gives me several errors.

............................................................

19:08:00: Compilación: "D:\Programacion\FreeBasic_1090\fbc.exe" -b "Prueba.bas" -exx -v -i "D:\Programacion\VisualFBEditor.1.3.1/./MyFbFramework"
FreeBASIC Compiler - Version 1.09.0 (2021-12-31), built for win32 (32bit)
Copyright (C) 2004-2021 The FreeBASIC development team.
standalone
target: win32, 486, 32bit
backend: gas
compiling: Prueba.bas -o Prueba.asm (main module)
assembling: D:\Programacion\FreeBasic_1090\bin\win32\as.exe --32 --strip-local-absolute "Prueba.asm" -o "Prueba.o"
linking: D:\Programacion\FreeBasic_1090\bin\win32\ld.exe -m i386pe -o "Prueba.exe" -subsystem console -T "D:\Programacion\FreeBasic_1090\lib\win32\fbextra.x" --stack 1048576,1048576 -s -L "D:\Programacion\FreeBasic_1090\lib\win32" -L "." "D:\Programacion\FreeBasic_1090\lib\win32\crt2.o" "D:\Programacion\FreeBasic_1090\lib\win32\crtbegin.o" "D:\Programacion\FreeBasic_1090\lib\win32\fbrt0.o" "Prueba.o" "-(" -lFB_Dll.dll -lfb -lgcc -lmsvcrt -lkernel32 -luser32 -lmingw32 -lmingwex -lmoldname -lgcc_eh "-)" "D:\Programacion\FreeBasic_1090\lib\win32\crtend.o"

D:\Programacion\FreeBasic_1090\bin\win32\ld.exe: Prueba.o:fake:(.text+0x80): undefined reference to `ADDNUMBERS@8'
linking failed: 'D:\Programacion\FreeBasic_1090\bin\win32\ld.exe' terminated with exit code 1
19:08:00: No construir el archivo. Elapsed Time: 0,08 Seconds

............................................................

I've read something about #inclib but I don't know how it works, and also several times in other codes I've gotten the

ld.exe: Prueba.o:fake:(.text+0x80): undefined reference

Another example that works in VB6 with the win32 API, but doesn't work in FreeBasic is..........

Private Declare Function timeGetTime Lib "winmm.dll" () As Long

Private Sub Form_Load()
Debug.Print "timer " & timeGetTime
End Sub


Thank you all very much and have a nice day!!!!!
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: How to call a DLL created with FreeBasic from FreeBasic

Post by srvaldez »

something like this

Code: Select all

#Include Once "windows.bi"
#Include Once "win/ole2.bi"

Extern "windows-ms"

Declare Function addnumbers ( _
	Byval operand1 As Integer, Byval operand2 As Integer ) As Integer

Declare Function uppercase(Byval arg As Zstring Ptr) As bstr

End Extern

#Inclib "FB_Dll"

Randomize( Timer( ) )

Dim As Integer x = Rnd( ) * 10
Dim As Integer y = Rnd( ) * 10

Print x; " +"; y; " ="; addnumbers( x, y )
don't know about the Function uppercase
about the last example on bottom of your post

Code: Select all

Extern "windows-ms"
	Declare Function timeGetTime Lib "winmm.dll" () As Long
end Extern

Print "timer " & timeGetTime
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to call a DLL created with FreeBasic from FreeBasic

Post by fxm »

Tolo68 wrote: Apr 01, 2022 19:35 Declare Function AddNumbers Lib "FB_Dll.dll" (ByVal operand1 As Integer, ByVal operand2 As Integer) As Integer
The DLL name must be specified without its extension:
Declare Function AddNumbers Lib "FB_Dll" (ByVal operand1 As Integer, ByVal operand2 As Integer) As Integer
(as for #inclib)
Tolo68
Posts: 105
Joined: Mar 30, 2020 18:18
Location: Spain

Re: How to call a DLL created with FreeBasic from FreeBasic

Post by Tolo68 »

Hello, thank you very much for the code!

----------------------------------
Extern "windows-ms"
Declare Function timeGetTime Lib "winmm.dll" () As Long
end Extern

Print "timer " & timeGetTime

----------------------------------

At first it kept giving me the error, but I searched for Google

Extern "windows-ms"

to see if there were more solutions, so I saw an example, but changing "windows-ms" por "windows"
I tried it, and the example of timeGetTime, At least I know how to use Windows DLLs!!!

My DLL still doesn't work, I'll recompile it making this change in its source code as well.

Now my question is... What is the difference between........Extern "windows-ms" y Extern "windows" ????

Thank you very much everyone, greetings!!!!
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to call a DLL created with FreeBasic from FreeBasic

Post by dodicat »

In the help file
https://www.freebasic.net/wiki/KeyPgExternBlock

Here I call your code forum.bas to get forum.dll
(Your code lines are the lines written in blue in the first post).

Code: Select all



#include once "win/ole2.bi"
#inclib "forum"

extern "windows-ms"
declare FUNCTION AddNumbers ( _
BYVAL operand1 AS INTEGER, BYVAL operand2 AS INTEGER ) AS INTEGER 

declare FUNCTION UpperCase(BYVAL arg AS ZSTRING PTR) AS BSTR 
end extern

print addnumbers(4,9)

print *cast(zstring ptr,uppercase("abcdefghijklmnopqrstuvwxyz"))
sleep

 
result

Code: Select all

 36
ABCDEFGHIJKLMNOPQRSTUVWXYZ
 
Tolo68
Posts: 105
Joined: Mar 30, 2020 18:18
Location: Spain

Re: How to call a DLL created with FreeBasic from FreeBasic

Post by Tolo68 »

Thank you Dodicat !!!.

I'll look at using the link code, I think this code is the one I used as an example.
I'll have to use it with Extern "windows-ms", because with Extern "windows", I saw that my Dll did not work in VB6 and VB.net 2010

I have Windows XP 32-bit, and I compile it with FreeBasic 1.0.9.0 / 32-bit, though I don't think this is why.

Thank you very much again!!!
Tolo68
Posts: 105
Joined: Mar 30, 2020 18:18
Location: Spain

Re: How to call a DLL created with FreeBasic from FreeBasic

Post by Tolo68 »

Thank you all!!!
I was able to solve it, the problem is that I created the DLL with an IDE, and surely I created the DLL wrong.
So compile the DLL directly with console commands....

fbc.exe -dll D:\Programacion\FreeBasic_Dll\add.bas

Now I can use it with...

Extern "windows-ms"
declare function AddNumbers lib "add.dll" ( byval a as integer, byval b as integer) as integer
end Extern

print AddNumbers(124,4)

...............

It already works for me with VB6, VB.net, and FreeBasic, even running the test Exe in linux, through Wine !!!!
I can't ask for more!!!!

Thank you all very much!!!
Xusinboy Bekchanov
Posts: 782
Joined: Jul 26, 2018 18:28

Re: How to call a DLL created with FreeBasic from FreeBasic

Post by Xusinboy Bekchanov »

Tolo68 wrote: Apr 03, 2022 15:11 Thank you all!!!
I was able to solve it, the problem is that I created the DLL with an IDE, and surely I created the DLL wrong.
So compile the DLL directly with console commands....

fbc.exe -dll D:\Programacion\FreeBasic_Dll\add.bas

Now I can use it with...
Is it because of UTF8 BOM? Have you tried saving in ASCII (Plain text)?
Tolo68
Posts: 105
Joined: Mar 30, 2020 18:18
Location: Spain

Re: How to call a DLL created with FreeBasic from FreeBasic

Post by Tolo68 »

Xusinboy Bekchanov wrote: Apr 03, 2022 16:03
Tolo68 wrote: Apr 03, 2022 15:11 Thank you all!!!
I was able to solve it, the problem is that I created the DLL with an IDE, and surely I created the DLL wrong.
So compile the DLL directly with console commands....

fbc.exe -dll D:\Programacion\FreeBasic_Dll\add.bas

Now I can use it with...
Is it because of UTF8 BOM? Have you tried saving in ASCII (Plain text)?
Hello Xusinboy, good morning!!!

I created the Dll with VisualFBEditor, from the "create dynamic library" menu, believing that the error would be due to this, I wrote the code with FBIDE, save the .bas, and compile it from the console.

I don't know if it will be, because of what you say about UTF8.
How do you change this in VisualFBEditor????

Today also searching the internet, I found something that I had been looking for for a long time, and I didn't know how to do it...

#cmdline " -dll "

for example

........
#cmdline " -dll "

EXTERN "windows-ms"

function SumaNumeros( byval a as integer, byval b as integer) as integer export
function = a + b
end function

END EXTERN

........

It's very good, because that's how I indicate at the beginning, that I compile this .bas code as a dll, and I don't have to be changing in the IDE if I want to create an exe or dll, so I have the 2 codes in the same ide, and I can go testing the dll.

By the way, I have written a suggestion for VisualFBEditor, there are the links of the photos.

Thanks!!!!
Greetings!!!!!
Post Reply