newObjects-logo.gif (1024 bytes) Scripting Host Control 2

Using the component

The ScriptManager2 ActiveX component can be used in many environments. It can be used to enrich a C++ or Delphi application, can be used from the script running in another scripting host in order to enrich the environment with additional features like libraries written in script language, it can be used as an alternative to the Windows Scripting Components in cases where you need more control over the script.

The trivial usage that shows the most important steps can be a simple Windows Scripting Host script that runs another script (see Architecture). The same steps can be followed when using component in another environments like ASP, GUI applications or plug-in for some product. Take a look at the call order diagram in the reference page.

First the instance of the component must be created (or accessed):

In JScript this looks:
var scpman = new ActiveXObject("newObjects.Scphost.ScpMan2");
In VBScript:
Set scpman = CreateObject("newObjects.Scphost.ScpMan2")
In C++ you can use SCPHOSTCLNT - and creation will be just:
SCPHOSTCLNT scpman;

Second step is to initialize the created object with a script engine. For example it can be JScript:

In JScript:
bOK = scpman.LoadEngine("JScript");
In VBScript:
bOK = scpman.LoadEngine("JScript")
In C++ using the SCPHOSTCLNT:
BOOL bOK = scpman.LoadEngine(_TEXT("JScript"));

Then some namespaces must be added. The namespaces are COM objects created somehow before this step. IDispatch pointer must be obtained and added to the namespaces collection of the ScriptManager2. Calling method Add name for the item must be supplied. Name will be used by the script to access the object.

In JScript:
var pos = scpman.Add("MyItem",someObjectPreCreated);
In VBScript:
pos = scpman.Add("MyItem",someObjectPreCreated)
In C++ using SCPHOSTCLNT:
int pos = scpman.Add(_TEXT("MyItem",pDispatchOfSomeObject);

More items can be added later too. Now some script code written in language of the selected script engine must be added. Script is must be a string containing code (see AddText for additional requirements). You can construct the script by loading the script text from several files, prompt user to write the script in-place and so on.

In JScript:
bOK = scpman.AddText(strSomeScriptCode);
In VBScript:
bOK = scpman.AddText(strSomeScriptCode)
In C++ using the SCPHOSTCLNT:
bOK = scpman.AddCode(szSomeScriptCode);

After completing with adding script operations (you can add the script text by calling the AddText method several times) the script must be started.

In JScript:
bOK = scpman.Run();
In VBScript:
bOK = scpman.Run
In C++:
bOK = scpman.Run();

After this step (if successful) all global methods in the script are defined and all global code is executed. Now the methods from the script can be called from the outside. Suppose that function MyFunction is defined in the script (with two parameters).

In JScript:
var result = scpman.script.MyFunction(p1,p2);
In VBScript:
result = scpman.script.MyFunction(p1,p2)
In C++ using the SCPHOSTCLNT and DISPCALLER:
DISPCALLER scpmandisp;
scpmandisp <= scpman.GetScriptDispatch();

scpmandisp << p2 << p1; // p2 and p1 are some variables of types supported by the << operator.
VARIANT result = scpmandisp.VariantCall(_TEXT("MyFunction"));

After completing with script usage the component can be reused - After calling the Close method.

In JScript:
scpman.Close();
In VBScript:
scpman.Close
In C++:
scpman.Close();

After that collection is not cleared. To clear it Clear method must be called.

The script usage from another running script may look strange but it can be a very powerful mechanism to build and use fast and flexible script libraries from the scripts. Good example of this is ASP (look at the example). Keeping the major part of the script in the ScriptManager2 object in the ASP Application and calling functions from it from the site ASP pages makes the site many times faster than a site that implements the same functionality in the include files. Yes the similar result can be reached with Windows Scripting Components but in this case control is not yours. Using the ScriptManager2 includes can be realized, intelligent script reload and any other feature that the application needs.

The most important thing that the developers must keep in mind is the script engine initialization (reinitialization). Once loaded script runs with very good performance - of course if the code makes calculation it will be times slower than a C++ code but in case of controlling features provided by the hosting application it will be better choice than using a lot of C++ code to make a simple calls to a dispatch interfaces. It will be a bit slower but much more flexible and easy to write, many possible errors will be eliminated. If the script is not reloaded between script function calls the performance of the script will be acceptable. Compared to the Unix shell scripts Windows Active Script is much more powerful and can be used in a critical environments (not real-time).

newObjects Copyright newObjects 2001-2005