◎ Basic structure of simple WSC Components

Source: Internet
Author: User
Tags cdata

WSC is a convenient way for Microsoft to use scripting language to create COM parts. It can be applied to applications that support com parts. WSC is written in XML. A standard WSC component involves XML tags, but fortunately its structure is very standard, understanding the structure, filling in the code, a WSC script component can work.
In my personal opinion, the WSC component is divided into three parts: Registration, defining attributes, methods, events, and implementation.

1. Registration part:
It mainly contains the information that needs to be filled in when registering a part to the Registry. It generally contains the part description, version information, progid, CLSID, and so on. The basic format is as follows:
<Registration
Description = "part description"
Version = "part version number"
Progid = "XXXX. XXX"/program id
CLSID = "XXXXXXXX-XXXX-xxxxxxxxxxxx"/>
Note:
① Registry is an XML tag.
② Progid is the identifier when vbs creates an instance by binding components through the Createobject function. If a component does not have a progid, it is almost unusable. For example:
Set OBJ = Createobject ("scripting. FileSystemObject ")
In the code, scripting. filesystemobjec is the progid.
③ CLSID is the unique identifier of a widget in the registry key. When the system registers the widget, information about it is stored in the registry. When the application reads the registration information, the application can locate and load the widget. You can use uuidgen.exe to randomly generate a program. Of course, if you do not specify CLSID, regsvr32 will automatically create one for it during system registration, but normally you should specify one. Since vcss are not installed on my computer and the uuidgen.exeand guidgen.exe files have always been a headache for me. Fortunately, Umu has provided a script to generate the Code as follows:
Dim objsltl
Set objsltl = Createobject ("scriptlet. typelib ")
Wscript. Echo objsltl. guid
Set objsltl = nothing

2. Define attributes, methods, and events
This section defines the attributes, methods, and events that users can use in the script. The basic structure is as follows:
<Public>
<Property name = "property 1">
<Get/>
<Put/>
</Property>

<Method name = "method 1">
<Parameter name = "parameter 1"/>
</Method>

<Event name = "Event 1">
<Parameter name = "parameter 1"/>
</Event>
</Public>
Public is the XML tag, and property, method, and event tags define component attributes, methods, and events respectively. The name tag defines its name, and the parameter tag defines its parameters. As the property tag, it has two sub-tags: <get/> <put/>, which define the read/write attribute method respectively. When read/write attributes are required, one of them is automatically executed. In the implementation section, they will appear in the form of put _ attribute name or get _ attribute name. If the <get/> flag is ignored, this attribute is only writable. If the <put/> flag is ignored, this attribute is read-only. As a vbs script, there are few opportunities to respond to events in the script, so I will not describe it in detail.

3. Implementation
All attributes, methods, and event implementation functions defined in the previous section must be included in the <SCRIPT> tag. The structure is as follows:
<Script language = "VBScript">
<! [CDATA [
Define public variables
Initialize
Function Name ()
Function implementation
End funciton
]>
</SCRIPT>
Regardless of the XML tag, we only need to modify the relevant part. The attributes and methods defined in the public section must be defined in this section.
Of course, some basic tags of WSC as an XML file still need to be known. The following mark description is taken from Microsoft.
<Component> and <package> elements: the <component> element contains a complete definition of a script part. Multiple <component> elements can appear in the same. WSC file, but these elements should be included in one <package> element.
<Registration> element: contains information used to register a script part as a com part.
<Public> element: contains the attributes, methods, and event definitions described by the script parts. These definitions point to variables or functions defined in an independent <SCRIPT> block.
<Implements> element: Specifies the COM interface handler for the script part, which determines which com part type the script part belongs. For example, by specifying <implements type = asp>, You can implement an ASP interface handler, and therefore you can access the ASP object model in the script component.
<Public> element: Specifies the script part to implement the COM automation interface handler. Therefore, you do not need to create the <implements> element for the automation processing program.
<SCRIPT> element: contains the script used to implement the logic of the script part, depending on the com part type created. For example, if you want to create a COM automation part, you can

<Public> declares attributes, methods, and events in the element, and then uses one or more <SCRIPT> elements to write scripts to define these content.
<Object> element: contains the information of an object used in the script, such as the information of another com part.
<Resource> element: contains values that should not be hardcoded into the script part code. Resource elements may include information that may be changed between versions, strings that may need to be translated, and other values.
<Reference> element: reference the type library to be used in the script.
<Comment> element: contains the text, description, or comments that will be ignored when the script component is analyzed and executed.

With this knowledge, let's look at the simplest example:

<? XML version = "1.0"?>
<Package>
<Component id = "test">
<Registration
Progid = "ID"
Description = "Description"
Version = "1"
CLSID = "{A66E243C-CF13-42AC-BE67-558F2FA82B39}"/>

<Public>
<Property name = "du"/>
<Get/>
<Method name = "xie"/>
</Public>

<Script language = "VBScript">
<! [CDATA [
Dim du
Function get_du ()
Du = get_du
End Function

Function Xie ()
Msgbox du
End Function
]>
</SCRIPT>
</Component>
</Package>

Call with code
Set test = Createobject ("ID ")
Test. Du = 5
Test. Xie
Display normal

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.