Coding a Custom Object with WSC - Adding objects and resources
(Page 3 of 5 )
Because Windows Script Components need to support COM-enabled applications outside of scripts alone, they have the ability to do things that you’re probably not used to seeing if you’re not an application developer. I’m not going to get into too much detail here, but I’ll show you a few things that you can use to your advantage.
</registration>
<object id="objShell" progid="Shell.Application">
<object id="objFso" progid="Scripting.FileSystemObject"/>
Below my registration section I’m adding two <object> elements. The <object> element allows me to specify an object that I want to be globally available in my component. I can provide the name I want to use, as well as specify the Prog ID. Here I’m just creating my Shell and FileSystem objects.
While using the <object> element makes my object available in the global namespace, that doesn’t provide much benefit over using CreateObject() in my script. However, I do not have access to the WScript.CreateObject() method in a script component. This means that objects can only be loaded using VBScript’s internal CreateObject() method. As you may or may not know, this has a limitation when it comes to event scripting. VBS’s CreateObject method cannot create an event handle in scripts. The <object> element can. All I need to do is set its optional “events” attribute to true and my component will begin handling any events fired by that object.
Below my <object> elements I’m now going to add some object references. Reference elements are used to create a reference to a resource’s Type Library.
<reference object="ADODB.Stream"/>
<reference object="Scripting.FileSystemObject"/>
If you’ve never developed in .Net or some higher-level programming language, you may not understand what resource references are used for. Many COM objects expose enumerations, or lists of constants, used by the object. In scripting we’re accustomed to defining these constants in our code because VBScript does not provide built-in support for enumerations. You’re probably used to writing code like this:
Const ForWriting = 2
Set objFile = objFso.OpenTextFile(strPath, ForWriting, True)
Here we’re defining a constant ForWriting with an integer value of 2 that the FileSystemObject uses when calling the OpenTextFile method. The fact is that this constant is already defined inside of the FileSystemObject, but VBScript can’t see it. By providing a reference to the FileSystemObject, I can write this type of code inside of my component:
Set objFile = objFso.OpenTextFile(strPath, ForWriting, True)
Because my component has a reference to the FileSystemObject, it has access to any constants that are exposed by its enumerations. I can safely use ForWriting without having to assign it first because it’s already been provided for me.
For those of you who only do general scripting, this may cause some problems, since you aren’t familiar with the enumerations presented by the various objects. You can dig around in the MSDN and get the enumeration lists, but it may or may not be worth your time. There are some script editors, however, that are capable of using this reference in conjunction with their auto-completion features. This will allow your script editor to auto-complete properties and methods for objects in the same way that it does for VBScript’s internal methods. The only editor that I’m aware of, however, that supports listing enumerated constants is SystemScripter 6.0.
Next: Registering and using your component >>
More Code Examples Articles
More By Nilpo