use FreeBasic DLL in PB

Share your advanced PureBasic knowledge/code with the community.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

use FreeBasic DLL in PB

Post by jack »

here's simple example of using a DLL made with FreeBasic

testFBDll.pb

Code: Select all

OpenConsole()

Define FBLibrary.i
Prototype.i Add_Strings(*result.string, *a.string, *b.string)
Prototype.l Add_Longs(a.l, b.l)
Prototype.d Add_Doubles(a.d, b.d)
Prototype.q Add_Quads(a.q, b.q)

FBLibrary = OpenLibrary(#PB_Any, "FBdll.dll")
If FBLibrary
   Global Add_Strings.i : Add_Strings.Add_Strings = GetFunction(FBLibrary, "Add_Strings")
   Global Add_Longs.i : Add_Longs.Add_Longs = GetFunction(FBLibrary, "Add_Longs")
   Global Add_Doubles.i : Add_Doubles.Add_Doubles = GetFunction(FBLibrary, "Add_Doubles")
   Global Add_Quads.i : Add_Quads.Add_Quads = GetFunction(FBLibrary, "Add_Quads")
Else
   PrintN("FBdll.dll not found")
   Input()
   End
EndIf

Define.s s, a, b
a="Καλώς ήρθατε στην"
b=" στην Purebasic"

s=Space(256)
Add_Strings(@s, @a, @b)
;MessageRequester(s,a+b) 
PrintN(s)
PrintN(Str(Add_Longs(123456789, 987654321)))
PrintN(Str(Add_Doubles(222222222222222.0, 333333333333333.0)))
PrintN(Str(Add_Quads(4611686018427387903, 4611686018427387904)))

Input()
CloseConsole()
FBdll.bas

Code: Select all

#cmdline "-dll -t 4096 -w all -arch native -gen gcc -Wc -O2 -v"

Extern "Windows-ms"
	Sub Add_Strings(Byval result As Wstring Ptr, Byval a As Wstring Ptr, Byval b As Wstring Ptr) Export
		*result=*a+*b
	End Sub
	
	Function Add_Longs(Byval a As Long, Byval b As Long) As Long Export
		Return a+b
	End Function

	Function Add_Doubles(Byval a As Double, Byval b As Double) As Double Export
		Return a+b
	End Function

	Function Add_Quads(Byval a As Longint, Byval b As Longint) As Longint Export
		Return a+b
	End Function
End Extern

you can get the 64-bit dll from https://drive.google.com/file/d/1Wb8W1v ... sp=sharing
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: use FreeBasic DLL in PB

Post by jack »

a slightly more useful example modeled after the UInt64 library from long ago
you may get the 64-bit dll from https://drive.google.com/file/d/1daUh4v ... sp=sharing
Uint64-test.pb

Code: Select all

OpenConsole()

Define FBLibrary.i
Prototype.l Uint64_ulong2Uint64(a.l, *b.Quad)
Prototype.l Uint64_add(*a.Quad, *b.Quad, *result.Quad)
Prototype.l Uint64_sub(*a.Quad, *b.Quad, *result.Quad)
Prototype.l Uint64_mul(*a.Quad, *b.Quad, *result.Quad)
Prototype.l Uint64_div(*a.Quad, *b.Quad, *result.Quad)
Prototype.l Uint64_mod(*a.Quad, *b.Quad, *result.Quad)
Prototype.l Uint64_cmp(*a.Quad, *b.Quad)
Prototype.l Uint64_val(*a.string, *b.Quad)
Prototype.l Uint64_str(*b.Quad, *a.string)

FBLibrary = OpenLibrary(#PB_Any, "Uint64.dll")
If FBLibrary
   Global Uint64_ulong2Uint64.i : Uint64_ulong2Uint64.Uint64_ulong2Uint64 = GetFunction(FBLibrary, "Uint64_ulong2Uint64")
   Global Uint64_add.i : Uint64_add.Uint64_add = GetFunction(FBLibrary, "Uint64_add")
   Global Uint64_sub.i : Uint64_sub.Uint64_sub = GetFunction(FBLibrary, "Uint64_sub")
   Global Uint64_mul.i : Uint64_mul.Uint64_mul = GetFunction(FBLibrary, "Uint64_mul")
   Global Uint64_div.i : Uint64_div.Uint64_div = GetFunction(FBLibrary, "Uint64_div")
   Global Uint64_mod.i : Uint64_mod.Uint64_mod = GetFunction(FBLibrary, "Uint64_mod")
   Global Uint64_cmp.i : Uint64_cmp.Uint64_cmp = GetFunction(FBLibrary, "Uint64_cmp")
   Global Uint64_val.i : Uint64_val.Uint64_val = GetFunction(FBLibrary, "Uint64_val")
   Global Uint64_str.i : Uint64_str.Uint64_str = GetFunction(FBLibrary, "Uint64_str")
Else
   PrintN("Uint64.dll not found")
   Input()
   End
EndIf

a.Quad 
b.Quad  
c.Quad 
Define.s s

;---------------------------------------------------
OpenConsole()

s="2000000000000" : UInt64_val(@s, a)
s="7" : UInt64_val(@s, b)

PrintN("A = 2000000000000")
PrintN("B = 7")

UInt64_add(a, b, c)
s=Space(64) : UInt64_str(c, @s) :PrintN("A + B = " + s)

Uint64_sub(a, b, c)
s=Space(64) : UInt64_str(c, @s) :PrintN("A - B = " + s)

UInt64_mul(a, b, c)
s=Space(64) : UInt64_str(c, @s) :PrintN("A * B = " + s)

UInt64_div(a, b, c)
s=Space(64) : UInt64_str(c, @s) :PrintN("A / B = " + s)

UInt64_mod(a, b, c)
s=Space(64) : UInt64_str(c, @s) : PrintN("A mod B = " + s)

result.l = UInt64_cmp(a, b) ; Result = 0 if a=b, -1 if a<b, 1 if a>b
PrintN("Compare A to B: (Return 0 if a=b, -1 if a<b, 1 if A>b) = " + Str(result)) 


PrintN("")
PrintN("Press Enter to Continue...")
Input()  
CloseConsole()
End
Uint64.bas

Code: Select all

#cmdline "-dll -t 4096 -w all -arch native -gen gcc -Wc -O2 -v"

/'
    Uint64 Library for PureBasic
    ----------------------------
'/

Type Uint64 As Ulongint Ptr

' Uint64 Ulong2Uint64 ------------------------------------------------------------
Extern "Windows-ms"
	Function Uint64_ulong2Uint64(Byval a As Ulong, Byval b As Uint64) As Long Export
		*b = Culngint(a)
		Return 1
	End Function

	' Uint64 Add ------------------------------------------------------------------
	Function Uint64_add(Byval a As Uint64, Byval b As Uint64, Byval result As Uint64) As Long Export
		*result = *a + *b
		Return 1
	End Function

	' Uint64 Subtract ------------------------------------------------------------------
	Function Uint64_sub(Byval a As Uint64, Byval b As Uint64, Byval result As Uint64) As Long Export
		*result = *a - *b
		Return 1
	End Function

	' Uint64 Multiply ------------------------------------------------------------------
	Function Uint64_mul(Byval a As Uint64, Byval b As Uint64, Byval result As Uint64) As Long Export
		*result = *a * *b
		Return 1
	End Function

	' Uint64 Divide ------------------------------------------------------------------
	Function Uint64_div(Byval a As Uint64, Byval b As Uint64, Byval divresult As Uint64) As Long Export
		*divresult = *a / *b
		Return 1
	End Function

	' Uint64 Mod ------------------------------------------------------------------
	Function Uint64_mod(Byval a As Uint64, Byval b As Uint64, Byval modresult As Uint64) As Long Export
		*modresult = *a Mod *b
		Return 1
	End Function

	' Uint64 Compare ------------------------------------------------------------------
	Function Uint64_cmp(Byval a As Uint64, Byval b As Uint64) As Long Export
		If (*a <> *b) Then
			Return Iif(*a < *b ,-1, 1)
		End If
		Return 0
	End Function

	' String to Uint64 ------------------------------------------------------------------
	Function Uint64_val(Byval mystring As Wstring Ptr, Byval a As Uint64) As Long Export
		*a=Culngint(*mystring)
		Return 1
	End Function

	' Uint64 to String ------------------------------------------------------------------
	Function Uint64_str(Byval a As Uint64, Byval result_string As Wstring Ptr) As Long Export
		Dim As Long length = Len(*result_string)
		Dim As String v = Str(*a)
		If (length*2+2)>=(Len(v)*2+2) Then
			*result_string=v
			Return 1
		Else
			Return 0
		End If
	End Function
end extern
example output

Code: Select all

A = 2000000000000
B = 7
A + B = 2000000000007
A - B = 1999999999993
A * B = 14000000000000
A / B = 285714285714
A mod B = 2
Compare A to B: (Return 0 if a=b, -1 if a<b, 1 if A>b) = 1
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: use FreeBasic DLL in PB

Post by Kwai chang caine »

This 2 codes works perfectly here :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply