MSScript Control is provided in msscript.ocx. It is a very handy tool to run VBScript/JScript without relying on CScript.exe or WScript.exe.
However, many current scripts have used WScript object a lot in WSH (Window Script Hosting) environment. For example, the following are used a lot.
1 2 3 4 | WScript.Echo("Hello")
WScript.Quit(0)
WScript.Echo(WScript.Arguments.Count)
WScript.Sleep(10) |
WScript.Echo("Hello") WScript.Quit(0) WScript.Echo(WScript.Arguments.Count) WScript.Sleep(10)
These are methods/properties of WScript object that is provided when you run scripts under WSH. If you use the MSScript Control to run the scripts in your program. e.g.,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | uses MSScriptControl_TLB; function _RunScript( const lang: string; const str: string ): boolean; var script: TScriptControl; s, lg: string; begin s := Trim(str); if (Length(s) <= 2) then begin Result := true; Exit; end; lg := Trim(lang); CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_SPEED_OVER_MEMORY); script := TScriptControl.Create(nil); try with script do begin Language := lg; Error.Clear; AllowUI := true; TimeOut := -1; UseSafeSubset := false; end; if (UpperCase(lg[1]) = 'V') then begin s := 'On Error Resume Next' + #13#10 + s; end; try script.ExecuteStatement(s); Result := true; except Result := false; end; finally script.Free; CoUninitialize; end; end; |
uses MSScriptControl_TLB; function _RunScript( const lang: string; const str: string ): boolean; var script: TScriptControl; s, lg: string; begin s := Trim(str); if (Length(s) <= 2) then begin Result := true; Exit; end; lg := Trim(lang); CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_SPEED_OVER_MEMORY); script := TScriptControl.Create(nil); try with script do begin Language := lg; Error.Clear; AllowUI := true; TimeOut := -1; UseSafeSubset := false; end; if (UpperCase(lg[1]) = 'V') then begin s := 'On Error Resume Next' + #13#10 + s; end; try script.ExecuteStatement(s); Result := true; except Result := false; end; finally script.Free; CoUninitialize; end; end;
unfortnately, the WScript object is not available. The alternative is to create a object that is IDispatch and add manually these functions/methods or properties in WScript object. And use script.AddObject(“WScript”, WScript, true) before script.ExecuteStatements(s) that will avoid most of the errors that is due to unpresence of WScript object.
Also you can add the definition of the WScriptA class which stimulates the commonly-used methods/properties in WScript. The following can be placed at the beginning of the VBScript or in your delphi code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | Class WScriptA Private fName Private fPath Private fFullName Public Sub Echo(Msg) MsgBox msg End Sub Public Sub Quit(Rtn) End Sub Public Property Get FullName FullName = fFullName End Property Public Property Let FullName(str) fFullName = str End Property Public Property Get Name Name = fName End Property Public Property Let Name(str) fName = str End Property Public Property Get Path Path = fPath End Property Public Property Let Path(str) fPath = str End Property Public Sub Sleep(n) End Sub End Class Dim WScript Set WScript = new WScriptA |
Class WScriptA Private fName Private fPath Private fFullName Public Sub Echo(Msg) MsgBox msg End Sub Public Sub Quit(Rtn) End Sub Public Property Get FullName FullName = fFullName End Property Public Property Let FullName(str) fFullName = str End Property Public Property Get Name Name = fName End Property Public Property Let Name(str) fName = str End Property Public Property Get Path Path = fPath End Property Public Property Let Path(str) fPath = str End Property Public Sub Sleep(n) End Sub End Class Dim WScript Set WScript = new WScriptA
Now you can at least use WScript.Echo in the MS Script Control. I would think this is the most commonly-used method and may save you lots of hassle.
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Running VBScript in MS ScriptControl
Next Post: Script32: A Freeware to Convert VBScript/JScript/HTA to Executables