PDA

View Full Version : Creating Oxygen DLLs using thinBasic



Charles Pegge
31-12-2013, 13:16
OxygenBasic can be used to create general-use DLLs. You can also test them at the same time within the envelope of a single thinBasic script.

To create independent DLLs, a run-time library source needs to be embedded. I have included RTL32 with the latest thinBasic_Oxygen here in examples/DLL/

http://www.thinbasic.com/community/showthread.php?t=12175

To expose any function, all you need to do is add export to its prototype.

The code for terminating the DLL (before the DLL is unloaded) is automatically generated.

Hello World Example


uses "oxygen"


dim src as string


src="


% filename "t.dll"
% dll
include "RTL32.inc"


function hello(byval name as string) as string, export
return "Hello "+name
end function


" 'end src


o2_basic src
if o2_errno then
msgbox 0, o2_error
stop
else
o2_exec
end if

'TEST


declare function hello lib "t.dll" alias "hello" (byval name as string) as string


msgbox 0,hello("World!")

ErosOlmi
02-01-2014, 15:15
This is another of the JIT magicians of Oxygen Basic !

Thanks a lot Charles
Eros

John Spikowski
02-01-2014, 18:37
This is another of the JIT magicians of Oxygen Basic !

Thanks a lot Charles
Eros

I bet a lot of that O2 DLLC magic could easily materialize in TB without the need for a rabbit or a hat.

RobbeK
03-01-2014, 12:05
Thank you very much Charles !
Rob

RobbeK
04-01-2014, 15:50
Hi all,

Wrote something using a DLL generated by OČ.
(attached)
However, can I allocate memory from and within the DLL that can be linked (later) by TB ? (Here the array is created in TB and linked by OČ -- I like to do it the reverse way -- if this is possible ?)
(or shorter, can a DLL allocate memory of its own that can be linked ?)
best Rob

John Spikowski
04-01-2014, 22:54
The O2 DLL is in the same process as TB and as long as you have a pointer to the memory you created in O2, it should addressable in TB.

mike lobanovsky
05-01-2014, 00:28
... unless you unload this DLL with an explicit or unfortunate call to FreeLibrary() in either thinBasic or OxygenBasic code. The process will continue to run but all the memory pointers created by the DLL will become invalid. From this standpoint, it's safer to allocate memory in the caller code rather than in the callee.

John Spikowski
05-01-2014, 02:04
... unless you unload this DLL with an explicit or unfortunate call to FreeLibrary() in either thinBasic or OxygenBasic code. The process will continue to run but all the memory pointers created by the DLL will become invalid.

In ScriptBasic using a C based extension module (like the IUP interface) I assign global variables in the callback routines and return to SB. I then call the (already loaded) extension module again returning these previously set global variables. Before I return to SB I reset them back to their default state. As Mike said, variable values assigned within an exportable function are lost on the return.

Charles Pegge
05-01-2014, 20:59
Normally one would expect the DLL to remain in process until the thinBasic program terminates.

An Oxygen DLL will automatically release all its global/static resources when the DLL is freed.

a procedure called finish() can be used to explicitly shut down any activities and resources, not controlled directly by the Oxygen runtime (GDIplus for example). If finish is not present in the source code, the following minimal implementation is automatically added in.
a

sub finish() external
terminate
end sub

this procedure is indirectly called when FreeLibrary is invoked by thinBasic to unload the DLL.