Say Hello in VBScript/Javascript COM Automation (WSC)


The VBScript/Javascript under Windows is very helpful, which is often used as a daily administration programming language.  WSH stands for Window Scripting Host. It is a Microsoft scripting-friendly environment, which comes with most of the windows installation by default. The interpreters, known as cscript.exe and wscript.exe are used to run the VBScript and JScript (Microsoft’s Javascript implementation) respectively. Because of COM (Component Object Model), these techniques become very powerful and convenient.

If you have a large set of code library written in VBScript/Javascript, why not do something easy to make it published, and thus be used/shared by other programming languages. WSC (Windows Scripting Component) is made for this purpose, which can turn your VBScript/Javascript easily into COM automation object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0"?>
<component>
<?component error="true" debug="true"?>
<registration description="Hello World" progid="Hello.World" version="0.01"></registration> 
<!--<implements id=Automation type=Automation>-->
<public>
    <method name="SayHello">
        <parameter name="who"/>
    </method>
    <method name="Version">
    </method>
</public>     
<!--</implements>--> 
 
<script language="VBScript"> 
<![CDATA[
 
Sub SayHello(who)
    Msgbox "Hello, " & who & "!"
End Sub
 
]]>
</script> 
 
<script language="JavaScript"> 
<![CDATA[
 
function Version() {
    return "0.1";
}
]]>
</script> 
</component>
<?xml version="1.0"?>
<component>
<?component error="true" debug="true"?>
<registration description="Hello World" progid="Hello.World" version="0.01"></registration> 
<!--<implements id=Automation type=Automation>-->
<public>
	<method name="SayHello">
		<parameter name="who"/>
	</method>
	<method name="Version">
	</method>
</public>     
<!--</implements>--> 

<script language="VBScript"> 
<![CDATA[

Sub SayHello(who)
	Msgbox "Hello, " & who & "!"
End Sub

]]>
</script> 

<script language="JavaScript"> 
<![CDATA[

function Version() {
	return "0.1";
}
]]>
</script> 
</component>

Oh, great, the above code is structured using XML.  Replace the value of progid with the desired name; and add API declaration followed by implementation. As you see, you can either implement using VBScript or Javascript or maybe some other languages if you have them installed e.g. Perl Script. There is no need to define the type of the parameters since in VBScript/Javascript, everything is loosely-typed (they can be adapted to different values at runtime). There is no need to specify whether it is a function or procedure. Just implement it as a sub if you don’t give a return value or function if there is a return value.

To register this component, save the above code in text-editor and rename the file ending with *.wsc

regsvr32-hello-world.wsc Say Hello in VBScript/Javascript COM Automation (WSC) code code library COM/OLE implementation javascript programming languages vbscript windows windows scripting host

Open a command shell (preferred with administration privilege) , navigate to the folder where you save the wsc file. Execute the command of regsvr32 HelloWorld.wsc (assume you have this name). A dialog will be popped up saying you have registered successfully with scrobj.dll 

The following VBScript shows how to invoke APIs from the COM we just defined.

1
2
3
4
5
6
7
8
9
Dim HelloWorld
 
Set HelloWorld = CreateObject("Hello.World")
 
HelloWorld.SayHello("justyy")
 
Msgbox HelloWorld.Version
 
Set HelloWorld = Nothing
Dim HelloWorld

Set HelloWorld = CreateObject("Hello.World")

HelloWorld.SayHello("justyy")

Msgbox HelloWorld.Version

Set HelloWorld = Nothing

hello.vbs Say Hello in VBScript/Javascript COM Automation (WSC) code code library COM/OLE implementation javascript programming languages vbscript windows windows scripting host

Please note that on X64, the above code will be defaulted to 64-bit code library and if you register it using C:\Windows\SysWOW64\regsvr32.exe you will have a 32-bit code library. The one in C:\Windows\System32\regsvr32.exe is for 64-bit library (on 64-bit OS).

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
611 words
Last Post: Coding Exercise - C ++ - Solve Integer Split using Dynamic Programming - 3 Implementation
Next Post: C++ Chess Board Printing in Windows Using CodePage 437 - Extended ASCII

The Permanent URL is: Say Hello in VBScript/Javascript COM Automation (WSC)

Leave a Reply