Msscriptcontrol (can be implemented in C # and other languages to call JavaScript code)

Source: Internet
Author: User

ScriptControl interface

Property name Type Note
Allowui BOOL Detects whether the user's interface element is allowed to run. If False, interface elements such as message boxes are not visible.
CodeObject Object The script is exposed to the object used by the host. Read-only.
Modules Modules The component library module that the host provides to the script. Read-only. (a COM component is usually a collection of objects in the form of object collection that can be left to the user two times for development, with each collection being a modules)
Language String Sets or gets the language that the scripting engine interprets, such as VBScript, JScript.
Name String The name of the scripting engine. Read-only.
Procedures Procedures Returns a collection of procedures defined in a module
Sitehwnd HWND To display the parent window handle of the UI in a script
State Enum Sets or returns the state of the control, and if 0, the control executes only the statement but does not forward the event, and 1 is the object forwarding event that is accepted by the joined control.
Timeout Long The time-out value of the control's execution script, 1 means no timeout
Usesafesubset BOOL Sets or returns whether the host program cares about security. The security level of the host program can be set from this property
Error Error Error object, when an error occurs, this property returns an Error object

Method name Parameters Function
Addcode Code as String To add a script to execute in the scripting engine
AddObject Name as String, object as Object, [addmembers as Boolean = False] Joins an object to the scripting engine so that the methods provided by the object can be used in the script, and so on.
Eval Expression as String Evaluation of an expression
Executestatement Statement as String Interpreting and executing script statements
Reset Discards all objects and code and resets the state property to 0.
Run ProcedureName as String, ParamArray Parameters () as Variant To run a specified procedure

Event name Function
Error Fires the event when an error occurs
TimeOut Occurs when the execution process times out

general methods of use1, add ScriptControl2 in the program, define a ScriptControl instance 3, call Addcode and so on to the script engine to add the script to execute. 4. Call run to execute the script function to execute. host-to-script communication1, Host script: This is a simple process, as long as the call to run can pass parameters to the script. 2. Script Call Host: The AddObject interface is called during initialization, exposing one or more objects of the host to the script, in which case the method of the host object can be called in the script. In VC + +, this object is not an ordinary C + + class, it must support automation, in the script can only invoke the Object Automation interface (in VB This process is relatively simple). Script DebuggingNone of the code written by anyone can be perfect at once, but the script is different because of the special location of the execution. First download a script debugger (I use Microsoft Script Debugger, there is a download on the MS website, another is said to be other as vj++), after installation, there is no boot menu on the menu. Debugger Start-upIt's easy to set breakpoints in a general high-level language, but how do you start debugging before starting the script debugger? In IE, the script error will automatically prompt for debugging, you can also add the interrupt code in the script (Vbscript:stop Javascript:debugger), but ScriptControl also need to modify the registry, you need to HKEY_ The value of JITDebug under Current_user\software\microsoft\windows script\settings is changed to 1 (default is 0)

The following is a complete code example of a C # reference:

usingSystem; usingMsscriptcontrol; usingSystem.Text; namespaceZZ {/// <summary>   ///Script Type/// </summary>    Public enumScriptLanguage {/// <summary>   ///JScript scripting language/// </summary>   JScript,/// <summary>   ///VBScript scripting language/// </summary>   VBscript,/// <summary>   ///JavaScript scripting language/// </summary>   JavaScript}/// <summary>   ///Script Run Error agent/// </summary>    Public Delegate voidRunerrorhandler (); /// <summary>   ///script run time-out agent/// </summary>    Public Delegate voidRuntimeouthandler (); /// <summary>   ///ScriptEngine class/// </summary>    Public classScriptEngine {PrivateScriptControl MSC; //defining script Run error events Public EventRunerrorhandler Runerror; //To define a script run timeout event Public EventRuntimeouthandler runtimeout; /// <summary>   ///constructor Function/// </summary>    PublicScriptEngine (): This(scriptlanguage.vbscript) {}/// <summary>   ///constructor Function/// </summary>   /// <param name= "Language" >Script Type</param>    PublicScriptEngine (ScriptLanguage language) { This. MSC =NewScriptcontrolclass ();  This. msc. Usesafesubset =true;  This. msc. Language =language.   ToString (); ((dscriptcontrolsource_event) This. msc). Error + =NewDscriptcontrolsource_erroreventhandler (Scriptengine_error); ((dscriptcontrolsource_event) This. msc). Timeout + =NewDscriptcontrolsource_timeouteventhandler (scriptengine_timeout); }   /// <summary>   ///Run the Eval method/// </summary>   /// <param name= "expression" >An expression</param>   /// <param name= "Codebody" >function Body</param>   /// <returns>return Value Object</returns>    Public ObjectEval (stringExpressionstringcodebody) {msc.   Addcode (Codebody); returnMSC.   Eval (expression); }   /// <summary>   ///Run the Eval method/// </summary>   /// <param name= "Language" >scripting language</param>   /// <param name= "expression" >An expression</param>   /// <param name= "Codebody" >function Body</param>   /// <returns>return Value Object</returns>    Public ObjectEval (ScriptLanguage language,stringExpressionstringcodebody) {   if( This. Language! =language) This. Language =language; returnEval (expression,codebody); }   /// <summary>   ///running The Run method/// </summary>   /// <param name= "Mainfunctionname" >Entry Function name</param>   /// <param name= "Parameters" >Parameters</param>   /// <param name= "Codebody" >function Body</param>   /// <returns>return Value Object</returns>    Public ObjectRun (stringMainfunctionname,Object[] Parameters,stringcodebody) {    This. msc.   Addcode (Codebody); returnMSc. Run (Mainfunctionname,refparameters)} 

Msscriptcontrol (can be implemented in C # and other languages to call JavaScript code)

Related Article

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.