PDA

View Full Version : Wrapping Library with DLL



LeftyG
08-02-2022, 01:04
Hello all,

New to thinBasic, but not Basic in general. I've coded in many different dialects of Basic over the years, QBasic, Visual Basic, PureBasic, etc. I tried looking at a few examples and I think I get it. I'm just wondering do I put the DLL in the same folder with the thinbasic wrapper resides, do I need the header files if its a C library?

Say I wanted to wrap SDL2, would I do something like this?



Const SDL_INIT_VIDEO = 32 'value converted to decimal from hex

Type Rect
x as single
y as single
w as single
h as integer
End Type

Declare Function SDL_Init Lib "SDL2.dll" "Alias SDL_Init" (ByVal flags as single) as single

ErosOlmi
08-02-2022, 13:20
Ciao LeftyG

welcome to thinBasic.

Yes you did it almost, only Alias must be outside string of the alias. And data type, docs seem telling that SDL_Init accepts an v and returns an Integer

Here an example. I do not know SDL but should work.


uses "console"


Const SDL_INIT_VIDEO = 32

Type Rect
x as single
y as single
w as single
h as integer
End Type


'https://wiki.libsdl.org/SDL_Init
Declare Function SDL_Init Lib "SDL2.dll" Alias "SDL_Init" (ByVal flags as UInt32) as Integer
'https://wiki.libsdl.org/SDL_Quit
Declare sub SDL_Quit Lib "SDL2.dll" Alias "SDL_Quit" ()


integer iResult
printl "Calling SDL_Init ..."
iResult = SDL_Init(SDL_INIT_VIDEO)
printl "Result: ", iResult
if iResult <> 0 Then
printl "An error occurred calling SDL_Init"
else
printl "Result was OK"
end if


WaitKey
SDL_Quit


Let me know.

Ciao
Eros

ErosOlmi
08-02-2022, 13:43
I'm just wondering do I put the DLL in the same folder with the thinbasic wrapper resides?


Yes, put the DLL in the same folder where you have your thinBasic script or your bundled thinBasic exe.
thinBasic will search for dll first in that folder.




Do I need the header files if its a C library?

You need to declare inside thinBasic the dll exported functions in order to use them. Keeping attention to correct data matching.

I think someone in the past used SDL library here and created a list of declares

jack
08-02-2022, 14:26
here are 3 links related to the SDL library, hope you find them useful
https://www.thinbasic.com/community/showthread.php?12068-Sdl-Library
https://www.thinbasic.com/community/showthread.php?12096-Sdl-Demo
https://www.thinbasic.com/community/showthread.php?12093-SDL-Heaven

LeftyG
08-02-2022, 23:16
Thanks. I figured out how to make a wrapper using a DLL.

xLeaves
25-05-2022, 08:03
Oh, we're kindred spirits, and this just requires a good translation of the header file. :D