Call the implementation code of VBScript, JavaScript, and other scripts in C #

Source: Internet
Author: User
Tags microsoft website

1. Use msscriptcontrol
Download Windows Script control from Microsoft website. It is an ActiveX (r) control, so I used InterOP in. net. After the download and installation are complete, create a new C # Windows Application Program Project: select the reference node in Solution Explorer, right-click the Add reference menu, and the Add reference dialog box is displayed. Click Browse to find the directory for installing Windows Script control and select msscript. ocx file. A msscriptcontrol component is added to the reference node. The following are all objects after InterOP.

Scriptcontrol provides simple interfaces for the host script engine that supports ActiveX (TM) scripts. Next, we will explain the attributes and methods of the scriptcontrol converted to the scriptcontrolclass class.
Attribute
Allowui attribute: applies to the user interface elements displayed by scriptcontrol itself or scirpt engine, and can be read and written.
Codeobject attribute: return object, which is used to call public members of a specified module. Read-only.
Error attribute: return the error object, which contains detailed information about the last error. Read-only.
Language attribute: sets or returns the name of the script language in use. Read/write.
Modules attribute: a set of return modules for the scriptcontrol object. Read-only.
Procedures attribute: return the process set defined in the specified module. Read-only.
Sitehwnd property: set or return the hwnd of the window, by executing the script Code This window is used to display the dialog box and other user interface elements. Read/write.
State attribute: sets or returns the mode of the scriptcontrol object. Read/write.
Timeout attribute: set or return time (MS). After this time, you can choose to abort the execution of the script code or allow the code to continue. Read/write.
Usesafesubset attribute: sets or returns a Boolean value to indicate whether the host application has confidentiality requirements. If the host application requires security control, usesafesubset is true; otherwise, it is false. Read/write.
Method
Addcode: add the specified code to the module. You can call the addcode method multiple times.
Addobject method: Make the Host Object Model Available to the script engine.
Eval method: calculates the expression and returns the result.
Executestatement method: run the specified statement.
Reset method: discard all script code and objects that have been added to scriptcontrol.
Run method: run the specified process.
Event
Error Event: This event occurs when a running error occurs.
Timeout event: This event occurs when the time specified by the timeout attribute is exceeded and the end is selected in the result dialog box.
Additional points
If the allowui attribute is set to false, statements such as the displayed dialog box do not work, such as msgbox statements in VBScript and alert in Javascript. If the executed scripts exceed the milliseconds set in timeout, the dialog box that exceeds the time reminder will not pop up, and vice versa. Resetting the language attribute will clear the code loaded by addcode. For the timeout attribute, when the time-out occurs, scriptcontrol checks the allowui attribute of the object, determine whether to allow display of user interface elements.
For more information, see the msdn documentation.
To make the control easier to use, I wrapped it in a scriptengine class. The following is the complete code: Copy code The Code is as follows: using system;
Using msscriptcontrol;
Using system. text;
Namespace ZZ
{
/// <Summary>
/// Script Type
/// </Summary>
Public Enum scriptlanguage
{
/// <Summary>
/// JScript Language
/// </Summary>
JScript,
/// <Summary>
/// VBSCRIPT script language
/// </Summary>
VBScript,
/// <Summary>
/// Javascript script language
/// </Summary>
Javascript
}
/// <Summary>
/// Script running error proxy
/// </Summary>
Public Delegate void runerrorhandler ();
/// <Summary>
/// Script running timeout proxy
/// </Summary>
Public Delegate void runtimeouthandler ();
/// <Summary>
/// Scriptengine class
/// </Summary>
Public class scriptengine
{
Private scriptcontrol MSC;
// Define a script running error event
Public event runerrorhandler runerror;
// Define the script running timeout event
Public event runtimeouthandler runtimeout;
/// <Summary>
/// Constructor
/// </Summary>
Public scriptengine (): This (scriptlanguage. VBScript)
{
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "language"> 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 the eval Method
/// </Summary>
/// <Param name = "expression"> Expression </param>
/// <Param name = "codebody"> function body </param>
/// <Returns> returned object </returns>
Public object eval (string expression, string codebody)
{
MSC. addcode (codebody );
Return MSC. eval (expression );
}
/// <Summary>
/// Run the eval Method
/// </Summary>
/// <Param name = "language"> script language </param>
/// <Param name = "expression"> Expression </param>
/// <Param name = "codebody"> function body </param>
/// <Returns> returned object </returns>
Public object eval (scriptlanguage language, string expression, string codebody)
{
If (this. Language! = LANGUAGE)
This. Language = language;
Return eval (expression, codebody );
}
/// <Summary>
/// Run the run Method
/// </Summary>
/// <Param name = "mainfunctionname"> name of the entry function </param>
/// <Param name = "Parameters"> parameter </param>
/// <Param name = "codebody"> function body </param>
/// <Returns> returned object </returns>
Public object run (string mainfunctionname, object [] parameters, string codebody)
{
This. MSC. addcode (codebody );
Return MSC. Run (mainfunctionname, ref para

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.