|
Every script host contains a special object that allows interaction between the VB Script interpreter and 4D. This VBScript object is named SHost.
With the SHost object you can:
- call 4D methods from VBS
- assign values to 4D variables from VBS code
- read and assign values of events parameters
from VB Script code.
Each script host has a different instance of the SHost object. A SHost object from a script host is independent of SHost object from another script host. For example Sender function will return different values for different script host.
A SHost object used inside a 4D event handling method has more functions then a SHost object used within the other methods.(see ParamCount, GetParam, SetParam, Sender).
SHost Functions:
§ SetReal 4DVarName , RealValue
Assigns a value to a 4D real variable.
The 4D variable must be a process or interprocess variable of type Real, and it must be declared before calling this method.
Example
`declaring the 4D Real type variable
C_Real(var)
`creating the script host
OCCreateScriptHost(“example”)
`transferring the Real value to the the 4D variable
OCExecuteScript(“example”,”SHost.SetReal “+Char(34)+”var”+Char(34)+” , 3.1415”)
`destroying the script host
OCDestroyScriptHost(“example”)
` displaying the value assigned from VBS
ALERT(String(var))
§ SetLong 4DVarName , LongValue
Assigns a value to a 4D Longint variable.
The 4D variable must be a process or interprocess variable of type Longint, and it must be declared before calling this method.
Example
`declaring the 4D Real type variable
C_Longint(var)
`creating the script host
OCCreateScriptHost(“example”)
`transferring the Longint value to the the 4D variable
OCExecuteScript(“example”,”SHost.SetLong “+Char(34)+”var”+Char(34)+” , 7 ”)
`destroying the script host
OCDestroyScriptHost(“example”)
` displaying the value assigned from VBS
ALERT(String(var))
§ SetText 4DVarName , TextValue
Assigns a value to a 4D Text variable.
The 4D variable must be a process or interprocess variable of type Text, and it must be declared before calling this method.
Example
`declaring the 4D Real type variable
C_Text(var)
`creating the script host
OCCreateScriptHost(“example”)
`transferring the Longint value to the the 4D variable
OCExecuteScript(“example”,”SHost.SetText “+Char(34)+”var”+Char(34)+” , "+char(34)+"this is a sample text"+char(34))
`destroying the script host
OCDestroyScriptHost(“example”)
` displaying the value assigned from VBS
ALERT(var)
§ Execute 4DString
Runs a 4D code line from VBS.
Example
OCCreateScriptHost(“example”)
`show a 4D alert
OCExecuteScript(“example”,”SHost.Execute “+Char(34)+”ALERT(STRING(Records in selection([Items])))”+Char(34))
OCDestroyScriptHost(“example”)
§ ParamCount EventId
Returns the number of parameters of an event.
EventId represents the Id assigned to the event. You can find this value in the first parameter (of type string) of the 4D event handling method.
ParamCount function is available only when an event is handled. SHost object does not have this function if it is not used inside an event handling method.
In other words this function can be called only from a 4D method that is set as event handler with OCSetEventHandler or OCSetAsynchHandler.
§ GetParam EventId , ParamNumber
Returns a parameter of the event.
The event parameters are numbered starting from 0.
EventId is the Id assigned to the event. You can find this value in the first parameter (of type string) of the 4D event handling method. .
GetParam function is available only when an event is handled. SHost object does not have this function if it is not used inside an event handling method.
In other words this function can be called only from a 4D method that is set as event handler with OCSetEventHandler or OCSetAsynchHandler.
§ SetParam EventId , ParamNumber , ParamValue
Set a parameter of the event.
EventId is the Id assigned to the event. You can find this value in the first parameter (of type string) of the 4D event handling method.
SetParam function is available only when an event is handled. SHost object does not have this function if it is not used inside an event handling method.
In other words this function can be called only from a 4D method that is set as event handler with OCSetEventHandler or OCSetAsynchHandler.
§ Sender EventId
Returns the OLE object that triggered the event.
EventId is the Id assigned to the event. You can find this value in the first parameter (of type string) of the 4D event handling method.
Sender function is available only when an event is handled. SHost object does not have this function if it is not used inside an event handling method.
In other words this function can be called only from a 4D method that is set as event handler with OCSetEventHandler or OCSetAsynchHandler.
§ Yield
The VBScript command SHost.Yield tells the 4D scheduler to execute one time-slice of all other running processes if one 60 nth of a second has elapsed since the last scheduler execution cycle.
SHost.Yield must be called within any VBScript looping structure .
If this is not done all other 4D processes will not execute for the duration of the loop.
This is like the IDLE 4th Dimension command.
Some examples:
· A VBScript code parses a document on disk, or manipulates a large amount of private data.
In this case calling SHost.Yield allows the execution of the other processes while processing a time-consuming operation.
In this case not calling SHost.Yield would give the user the feeling that the program is “frozen” because other processes would not be able to answer to the events.
§ Eval4D 4DExpression
Evaluates the 4D expression contained by the "4DExpression" string, and returns the result.
Supported 4D types are :
Real, Longint, Integer, String, Text, Boolean, Date, Time.
This command can be used to transfer values from 4D into VBS, using VBS code.
Example
C_REAL(r4DVariable)
r4DVariable:=1/2
lError:=OCCreaScriptHost ("example")
lError:=OCRunScript ("example";"VBSVariable=SHost.Eval4D("+Char(34)+"r4DVariable+1"+Char(34)+")")
This code sample transfers into the VBScript variable named "VBSVariable" the value of the "r4DVariable+1" 4D expression, which is 1.5 .
|