Main Menu

SBT Embedding

Started by root, May 19, 2026, 07:11 PM

Previous topic - Next topic

root

ScriptBasic can be embedded into other applications as a DLL interface. I wrote the SBT extension module which embeds ScriptBasic in ScriptBasic to help others with the ScriptBasic API. I also added the ability to embed ScriptBasic as a thread.

The following example demonstrates accessing the ScriptBasic API

' SBT Demo

IMPORT sbt.sbi

sb_code = """
FUNCTION prtvars(a, b, c)
  PRINT a,"\\n"
  PRINT FORMAT("%g\\n", b)
  PRINT c,"\\n"
  prtvars = "Function Return"
END FUNCTION

a = 0
b = 0
c = ""
"""

sb = SB_New()
SB_Configure sb, "C:/Windows/SCRIBA.INI_32"
SB_Loadstr sb, sb_code
SB_NoRun sb
' Call function before running script
funcrtn = SB_CallSubArgs(sb,"main::prtvars", 123, 1.23, "One,Two,Three")
PRINT funcrtn,"\n"
' Run script initializing globals
SB_Run sb, ""
' Assign variables values
SB_SetInt sb, "main::a", 321
SB_SetDbl sb, "main::b", 32.1
SB_SetStr sb, "main::c", "Three,Two,One" & CHR(0)
' Call function again with variables assigned in the previous step
SB_CallSubArgs sb, "main::prtvars", _
          SB_GetVar(sb, "main::a"), _
          SB_GetVar(sb, "main::b"), _
          SB_GetVar(sb, "main::c")
SB_Destroy sb

Output

C:\ScriptBasic\examples>sbc sbt_demo.sb
123
1.23
One,Two,Three
Function Return
321
32.1
Three,Two,One

C:\ScriptBasic\examples>


This is an example of embedding ScriptBasic as a thread process and using the MT extension module to share a common status variable among processes.

Code (main) Select

' SBT Main

IMPORT mt.sbi
IMPORT sbt.sbi

SB_ThreadStart("sbt_thread.sb", "","C:/Windows/SCRIBA.INI")

FOR x = 1 TO 10
  PRINT "M:",x,"\n"
  sb_msSleep(20)
NEXT

SB_msSleep(1000)

PRINT "Thread ",mt::GetVariable("thread_status"),"\n"

Code (thread) Select
' SBT Thread

IMPORT mt.sbi
IMPORT sbt.sbi

FOR x = 1 TO 10
  PRINT "T:",x,"\n"
  SB_msSleep(20)
NEXT

mt::SetVariable "thread_status","Completed"

SB_ThreadEnd

Output

C:\ScriptBasic\examples>sbc sbt_main.sb
T:1
T:2
M:1
T:3
M:2
M:3
T:4
M:4
T:5
M:5
T:6
T:7
M:6
M:7
T:8
T:9
M:8
M:9
T:10
M:10
Thread Completed

C:\ScriptBasic\examples>