Support > General Discussions

Embedding into application

<< < (2/4) > >>

Zulfi.Ali:
Thanks for your help... i managed to create a call back function and display the output in an edit control box...but i had to set the embedder pointer as well since i used a global callback function...

My next task is to exchange data between the C environment and the script... i need to pass variables into the script and then get results back from the script... i could not find any example of how this is to be achieved so once again i need ur assistance...

how can i handle the data exchange if my variable count is high? I need to exchange only doubles as the script is meant for user defined calculations...

Support:
Another BCX embedded example to help you take your next step. Notice this example shows the results if you just load your script and access functions and variables and what the results are if you run the script.

BCX_embed4.bas

--- Code: ---#include "\scriptbasic\source\scriba.h"
#include "\scriptbasic\source\getopt.h"

$LIBRARY "libscriba.lib"

' NOTE: Windows specific above this line and generic BCX below.

Dim pProgram As pSbProgram
Dim v As long
Dim f1 As long
Dim f2 As long
Dim dVal=11 as long
Dim cArgs As long
Dim ReturnData As SbData
dim ArgData[4] As SbData

' LOADING AND RUNNING THE PROGRAM
pProgram = scriba_new(malloc, free)
scriba_SetFileName(pProgram, "E03.bas")
scriba_LoadSourceProgram(pProgram)
scriba_NoRun(pProgram)

'' ACCESSING GLOBAL DATA
v = scriba_LookupVariableByName(pProgram, "main::a")

scriba_SetVariable(pProgram, v, 2, 500, 0, "", 0)

'' CALLING SIMPLE SUBROUTINE
f1 = scriba_LookupFunctionByName(pProgram, "main::dprint")
scriba_Call(pProgram, f1)

' CALLING FUNCTION, RETURNING DATA AND GETTING ALTERED PARAMS
f2 = scriba_LookupFunctionByName(pProgram, "main::eprint")

' SETUP ARGUMENTS

for cArgs=0 to 3
  ArgData[cArgs].type = SBT_DOUBLE
  ArgData[cArgs].size = 0
  ArgData[cArgs].v.d = dVal+cArgs
next

scriba_CallArgEx(pProgram, f2, &ReturnData, cArgs, ArgData)
print "Return type:",ReturnData.type
print "Value:";

' READ RETURNED VALUE
select case ReturnData.type
 case SBT_UNDEF  :                print "Undefined"
 case SBT_DOUBLE :                print ReturnData.v.d
 case SBT_LONG   :                print ReturnData.v.l
 case =SBT_STRING or =SBT_ZCHAR : print (CHAR PTR)ReturnData.v.s
end select

scriba_destroy(pProgram)

--- End code ---

ScriptBasic - E03.bas

--- Code: ---' ----------------
' GLOBAL VARIABLES
' ================
'
a=42
b=sqr(2)
s="hello world!"

q=dprint()
q=eprint(1,2,3,4)

'print "Enter: "
'line input q

' ---------
' FUNCTIONS
' =========

function dprint
  sp="  "
  cr="\n"
  tab=chr$(9)
  print "Dprint Globals: " & s & sp & a & sp & b & cr
  ' line input q
  dprint=1
end function

function eprint(a,b,c,d)
  sp="  "
  cr="\n"
  tab=chr$(9)
  print "Eprint Args: " & a & sp & b & sp & c & sp & d & cr
  ' line input q
  ans=a+b+c+d
  eprint="Sum = " & ans
end function

--- End code ---

Results:(scriba_NoRun)

--- Code: ---C:\Program Files\BCX\sb>BCX_embed4
Dprint Globals:   500
Eprint Args: 11.000000  12.000000  13.000000  14.000000
Return type: 3
Value:Sum = 50

C:\Program Files\BCX\sb>

--- End code ---

Results: (Scriba_Run)

--- Code: ---C:\Program Files\BCX\sb>BCX_embed4
Dprint Globals: hello world!  42  1.414214
Eprint Args: 1  2  3  4
Dprint Globals: hello world!  500  1.414214
Eprint Args: 11.000000  12.000000  13.000000  14.000000
Return type: 3
Value:Sum = 50

C:\Program Files\BCX\sb>

--- End code ---

Zulfi.Ali:
Hi,

Using your examples, I have managed to pass values to my script variables but I have a problem; I need to pass arrays into the script.  How can I achieve that?  I tried to access variables using "main::x[0]" but this didnt work.

Suggestions needed...

Thanks

Zulfi.Ali:
Hi,

I am still struggling with the task of setting array elements from my C program... Is it possible? If not then I would have to abandon my work and look at other alternatives for a scripting language.

Help needed urgently

Thanks

Support:
Have you looked at the developer docs for embedding ScriptBasic?

The ScriptBasic WIki is a great place for information.

http://www.scriptbasic.org/wiki

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version