Enable script support for applications

Source: Internet
Author: User

Enable script support for applications

Relationship between Script Host, script engine, and script
To implement the script support function based on COM components, we need to correctly understand the script parties and their relationships. The script concepts include Script Host, script engine, and script ).

What is a script host? It sounds like a biological term. Similar to the concept of biology, we can simply think of it as the functional object to be controlled by the scripts we write and the functional components we want to provide to end users. For example, word is a text processing system provided by Microsoft. It adopts a com-based software structure. If we write some scripts for word (which is commonly referred to as "macro "), then, the word object attached to the script is the Script Host. The final performer of the script function is the Script Host. Any self-developed application we write can be considered as a host and can be embedded with Script interpretation capabilities. Of course, the host should also be Programming Based on COM. On the other hand, a script is the code that requests or controls services on the host.

The script engine is a "bridge" between the Script Host and script ". The script engine obtains the script, interprets it according to the syntax, controls the properties of the objects provided by the Host Program, and calls related methods to implement the functions required by the script. Providing available objects to scripts and interpreting scripts to achieve secondary development is the main function of the engine.

The relationship between the three can be expressed in the following figure.

--------------------------------------------------------------------------------

Provide objects to the script control

--------------------------------------------------------------------------------

Control objects provided by the host

Get the script to explain execution from the input part

 

 

Attributes, methods, and events of a script engine object
The script engine is provided to users as an ActiveX control. before using it, let's take a look at the properties and methods of this control.

Attribute:

Allowui: It can be read and written, and has a Boolean value. It checks whether user interface elements can be run. If it is false, interface elements such as message boxes are invisible.

Codeobject: return the specific public member object called by the host. Read-only.

Modules: read-only. Returns the Component Library module provided by the host to the script. COM components generally provide users with a collection of objects that can be left for secondary development. Each collection is a modules (module ).

Language: set or obtain the language interpreted by the script engine, such as VBScript and JScript.

Name: the name of the returned module, process, or object. It is read-only.

Procedures: the process defined in the return module. It is read-only.

Sitehwnd: Window handle. If this control is created using an ActiveX control, this property is the control container. If it is created using an automated object, it is 0, that is, the desktop. This attribute can be read and written.

State: sets or returns the status of the control. If it is 0, the control only executes statements but does not forward events. If it is 1, it is the object forwarding event accepted by the added control.

Timeout: set or return the timeout value of the corresponding script of the control. When the timeout value arrives, a timeout event is triggered. It can also be set to-1, indicating no timeout setting.

Usesafesubset: sets or returns whether the host Program cares about security. The security level of the Host Program can be set from this attribute.

Error: Error object. When an error occurs, this attribute returns an error object.

Method:

Addcode: add some code to a module. The format is addcode code, which can be called multiple times.

Addobject: adds an object to a module in the format of scriptcontrol. addobject (name, object [, addmembers]), name indicates the name of the object to be added, object indicates the actual object, and addmembers indicates whether the object is a global object, false is a local object. These objects can be used in scripts only after objects are added to this object.

Eval: Evaluate expression. Format: object. eval (expression ).

Executestatement: interpret and execute script statements. Format: object. executestatement statement.

Reset: discard all objects and code. The state attribute is 0.

Run: run a specified process. Format: object. Run (procedurename, parameters (), procedurename is the name of the process to be run, parameters () is

Event

There are only two events: Error and timeout. The former can be used for error capture, while the latter occurs when the script does not receive a response.

VB development instance
Suppose we want to develop an editing software in VB. This system is obviously the host of the script technology mentioned above. In order to support secondary development, the system is designed to be based on COM, it also provides users with two objects: "word" and "mybox ". In fact, in order to simplify the problem and achieve the purpose of demonstration, this example does not design complicated code by itself, the objects provided to users are all "ready-to-use" Word ", which is the word in the office installed in your system, however, it appears in the "My provided object" image by "turning hands". In office VBA, all the method attributes of word can be provided to users in the script function of the system; "mybox" is actually a Textbox Control added during design, but it is provided to the user in my text box. The interface is simple (simple? Opportunistic ?), Try not to divert everyone's attention to understand the implementation of script technology described in this article. The code and comments are as follows:

Version 5.00:

'Use an Automation server and an ActiveX control to support this example.

Object = "{0e59f1d2-1fbe-11d0-8ff2-00a0d10038bc} #1.0 #0"; "msscript. ocx"

Begin VB. Form form1

Caption = "demo system with Script support"

Clientheight = 1, 4335

Clientleft = 60

Clienttop = 345

Clientwidth = 4680

Linktopic = "form1"

Scaleheight = 4335

Scalewidth = 4680

Startupposition = 3'' Windows Default

Begin VB. textbox text2

Height = 1095

Left = 1, 120

Multiline =-1 ''true

Tabindex = 2

TEXT = "VBScript. frx": 0000

'The user can enter the code that controls the attributes of this object (My object name is mybox) through the script code window, or start and control the Office word in your system through the word object.

Tooltiptext = "Enter the code in the script code box to control the attributes of this object"

Maximum = 3120

Width = 2415

End

Begin VB. textbox text1

Height = 2295

Left = 1, 120

Multiline =-1 ''true

Scrollbars = 3' both

Tabindex = 1

TEXT = "'enter the script code here"

Tooltiptext = "you can control two objects provided by the system in the Code: Word and mybox. "

Maximum = 480

Width = 4335

End

Begin VB. commandbutton command1

Caption = "execute script code"

Beginproperty font

Name = "Ms sans serif"

Size = 9.75

Charset = 0

Weight = 400

Underline = 0'' false

Italic = 0'' false

Strikethrough = 0'' false

Endproperty

Height = 735

Left = 1, 2760

Tabindex = 0

Maximum = 3480

Width = 1815

End

'Default attributes of the script object

Begin msscriptcontrolctl. scriptcontrol scriptcontrol1

Left = 0

Maximum = 2160

_ Extentx = 1005

_ Extenty = 1005

Allowui =-1 ''true

End

Begin VB. Label label1

Caption = "Enter your VB code here"

Beginproperty font

Name = "Ms sans serif"

Size = 9.75

Charset = 0

Weight = 400

Underline = 0'' false

Italic = 0'' false

Strikethrough = 0'' false

Endproperty

Height = 255

Left = 1, 120

Tabindex = 3

Maximum = 120

Width = 3255

End

End

Attribute vb_name = "form1"

Attribute vb_globalnamespace = false

Attribute vb_creatable = false

Attribute vb_predeclaredid = true

Attribute vb_exposed = false

Private sub commandementclick () 'When command1 is clicked

Scriptcontrol1.executestatement text1.text 'interpret and execute the script

End sub

Private sub form_load () 'form Loading

Dim ob as object' defines an object variable

Set Ob = Createobject ("word. application") 'creates an Automation Object and references the programming ID

Scriptcontrol1.state = connected ': sets the status of the script control.

Scriptcontrol1.addobject "Word", ob' adds an acceptable object "word" to the script object"

Scriptcontrol1.addobject "mybox", text2 Add a basic VB Control textbox in the form

End sub

Instance running requirements: VB6.0 and pwin98. In principle, you must install word97 in your system. However, if you want to install Createobject ("word. application ") Statement modification, put" word. application "(word97's progid in the Registry) is replaced with the progid of any existing Automation Object in your system. After starting this example, you can enter the following code in the script input box to observe the effect, or write your own code based on understanding the attributes and methods of the objects provided by the script:

Word. Visible = true' make the word object appear, and use it like starting it through conventional methods.

Mybox. Text = "this is the content set in the script"

Additional points
About the objects provided by the host.
You can add any custom objects when writing the host. In this example, these objects can be objects written in VB and controls provided by VB (including ActiveX controls), automatic objects in the system (or even other controls and COM objects written by you, such as Delphi), but for custom objects, the addobject method must be used to add them to the script control modules.

Control of runtime objects
When using object. xxx to reference object methods and attributes, you must confirm the validity of the entered code. The so-called "validity" means that the attributes and Methods referenced when the script control interprets the Code must be available or valid (some attributes are read-only or write-only, some write methods cannot be called in some States. No error capture is performed in this example. If you are interested, you can design it yourself.

Use of the script engine in C ++
In addition to scriptcontrol in VB, scriptcontrol can also be used in C ++. In visual studio97, some special interfaces must be implemented using the script function, which is cumbersome. Using this control can save the interface limitations supported by the script. Many of the independent software we developed did not use the script support function, causing inconvenience in software use and secondary development. For example, wps97 does not have a macro function similar to word97, it should be said that it is a gap between our domestic software and the world software (because we do not have wps2000 at hand, we cannot comment on it ). In general, the development software now uses C ++ to easily implement COM, and the script control can be used to easily implement script support and add colors to your own software. Why not?

Article Source: DIY tribe (http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/2007126/90798.html)

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.