Call the implementation code of VBScript javascript and other scripts in c sharp

  • 2020-05-05 11:50:00
  • OfStack

one. Use MSScriptControl
Go to the Microsoft web site and download Windows Script Control, which is an ActiveX (R) control, so I used Interop in.NET. After downloading and installing, create a new Windows application project of C#, select the reference node in solution explorer, right-click and select add reference menu, the dialog box of add reference pops up, click browse to find the directory where Windows Script Control is installed, and select msscript.ocx file to confirm. Then an MSScriptControl component will be added under the reference node. Here are all the objects after Interop.


ScriptControl provides a simple interface to host Script engines that support ActiveX(TM) Script. Let's go on to explain the properties and methods of ScriptControl that are converted to the ScriptControlClass class.
Attribute
AllowUI attributes: apply to user interface elements displayed by the ScriptControl itself or the Scirpt engine, read and write.
CodeObject property: returns an object that is used to invoke the public member of the specified module. Read-only.
Error property: returns an Error object with details about the last error that occurred. Read-only.
Language property: sets or returns the name of the Script language being used. Read/write.
Modules property: returns the collection of modules for the ScriptControl object. Read-only.
Procedures property: returns the collection of procedures defined in the specified module. Read-only.
SitehWnd properties: sets or returns hWnd to the window that displays dialog boxes and other user interface elements by executing the Script code. Read/write.
State properties: sets or returns the mode of the ScriptControl object. Read/write.
Timeout properties: sets or returns a time (milliseconds) after which the user can choose to abort the execution of the Script code or allow the code to continue. Read/write.
UseSafeSubset property: sets or returns an Boolean value indicating whether the host application has confidentiality requirements. If the host application requires security control, UseSafeSubset is True, otherwise False. Read/write.
Methods
AddCode method: adds the specified code to the module. The AddCode method can be called multiple times.
AddObject method: make the host object model available to the Script engine.
Eval method: evaluates the expression and returns the result.
ExecuteStatement method: executes the specified statement.
Reset method: discard all Script code and objects that have been added to ScriptControl.
Run method: runs the specified procedure.
Event
Error event: this event occurs when a runtime error occurs.
Timeout event: this event occurs when the time specified by the Timeout attribute is exceeded and the user selects End in the results dialog.
A few points to add If the AllowUI attribute is set to false, statements such as the display dialog box will not work, such as MsgBox statement in VBScript, alert in javascript, etc., and if the script is executed beyond the number of milliseconds set by TimeOut, it will not jump out of the exceeded time reminder dialog box. Resetting the Language property empties the code that AddCode loaded; For the TimeOut attribute, when a timeout occurs, ScriptControl checks the AllowUI attribute of the object to see if it is allowed to display user interface elements.
If you need more details, you can check out the MSDN documentation.
To make the control easier to use, I wrapped it with an ScriptEngine class. Here's the full code:

using System; 
using MSScriptControl; 
using System.Text; 
namespace ZZ 
{ 
/// <summary> 
///  The script type  
/// </summary> 
public enum ScriptLanguage 
{ 
/// <summary> 
/// JScript Scripting language  
/// </summary> 
JScript, 
/// <summary> 
/// VBscript Scripting language  
/// </summary> 
VBscript, 
/// <summary> 
/// javascript Scripting language  
/// </summary> 
javascript 
} 
/// <summary> 
///  The script runs the error proxy  
/// </summary> 
public delegate void RunErrorHandler(); 
/// <summary> 
///  The script runs the timeout agent  
/// </summary> 
public delegate void RunTimeoutHandler(); 
/// <summary> 
/// ScriptEngine class  
/// </summary> 
public class ScriptEngine 
{ 
private ScriptControl msc; 
// Define script run error events  
public event RunErrorHandler RunError; 
// Define a script run timeout event  
public event RunTimeoutHandler RunTimeout; 
/// <summary> 
/// The constructor  
/// </summary> 
public ScriptEngine():this(ScriptLanguage.VBscript) 
{ 
} 
/// <summary> 
///  The constructor  
/// </summary> 
/// <param name="language"> The script type </param> 
public ScriptEngine(ScriptLanguage language) 
{ 
this.msc = new ScriptControlClass(); 
this.msc.UseSafeSubset = true; 
this.msc.Language = language.ToString(); 
((DScriptControlSource_Event)this.msc).Error += new DScriptControlSource_ErrorEventHandler(ScriptEngine_Error); 
((DScriptControlSource_Event)this.msc).Timeout += new DScriptControlSource_TimeoutEventHandler(ScriptEngine_Timeout); 
} 
/// <summary> 
///  run Eval methods  
/// </summary> 
/// <param name="expression"> expression </param> 
/// <param name="codeBody"> The body of the function </param> 
/// <returns> The return value object</returns> 
public object Eval(string expression,string codeBody) 
{ 
msc.AddCode(codeBody); 
return msc.Eval(expression); 
} 
/// <summary> 
///  run Eval methods  
/// </summary> 
/// <param name="language"> Scripting language </param> 
/// <param name="expression"> expression </param> 
/// <param name="codeBody"> The body of the function </param> 
/// <returns> The return value object</returns> 
public object Eval(ScriptLanguage language,string expression,string codeBody) 
{ 
if(this.Language != language) 
this.Language = language; 
return Eval(expression,codeBody); 
} 
/// <summary> 
///  run Run methods  
/// </summary> 
/// <param name="mainFunctionName"> Entry function name </param> 
/// <param name="parameters"> parameter </param> 
/// <param name="codeBody"> The body of the function </param> 
/// <returns> The return value object</returns> 
public object Run(string mainFunctionName,object[] parameters,string codeBody) 
{ 
this.msc.AddCode(codeBody); 
return msc.Run(mainFunctionName,ref para

Related articles: