Günter Born

WSHExtend Programmers Reference

The following document describes the WinExt object model of the WSHExtend ActiveX control. WSHExtend is an ActiveX control which extend the WSH with additional objects, methods and properties. I have developed this control within my Windows Scripting Host Tutorial book project (Wrox). To allow the public to use the extensions as early as possible, I have decided to bring the OCX file and this documents online. The source code and many samples will be released with the book.

The WinExt object model

The ActiveX control WSHExtend (WinExtend.ocx) contains the object WinExt, which extends the Windows Scripting Host. Below is a short reference of the WinExt object model, describing the methods and properties.

Using the object

The object may be created both in VBScript and in JScript with the following statement:

Dim objAdr
obj = WScript.CreateObject("WSHExtend.WinExt")

Then you may use the object variable objAdr to access the methods and properties.

The WSHKeyExist method

This method may be used only in VBScript and checks whether a Registry key exists. The method uses the following syntax:

obj.WSHKeyExist(Root, key ) = bstatus

The parameter Root contains the code for the Root key, which must be defined in VBScript as:

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006

The parameter key contains the key/value whose existence may be checked. The method returns either true (exist) or false (entry not found).

' Create WSHShell object, required for Registry access
Set WSHShell = WScript.CreateObject("WScript.Shell")

' Create object for the WSH extension WSHKeyExist
Set objAdr = CreateObject("WSHExtend.WinExt")

' Ask user for a file name extension
key1 = InputBox ("Please enter a file name extension " + _
"(for instance .bmp)", "File name extension",".bmp")

If key1 = "" Then WScript.Quit()

' Test, whether key exists
If objAdr.WSHKeyExist (HKEY_CLASSES_ROOT, key1) Then
   key = WSHShell.RegRead ("HKCR\"+key1+"\")

The WSHKeyExist1 method

Same function as WSHKeyExist, but supports both VBScript and JScript. The method uses the following syntax to check the existence of a key/value:

obj.WSHKeyExist1(Root, key ) = bstatus

The parameter Root contains the code for the Root key as a string (may be "HKCR", "HKCU", "HKLM" or "HKU"). The parameter key contains the key or value, whose existence may be checked. The method returns true (entry found) or false (entry not found).

The WSHInputBox method

This method implement the VBScript InputBox function for all WSH languages (JScript for instance). The method uses the following syntax:

obj.WSHInputBox(Prompt, title, Default) = result

Prompt contains the string which shall be displayed. In title we submit the title text for the dialog box window. The parameter Default is a Variant and defines the value shown in the input box of the dialog. The method submits the parameters directly to the Visual Basic InputBox function and returns the user input.

Dim objAdr
objAdr = WScript.CreateObject("WSHExtend.WinExt")
WScript.Echo objAdr.WSHInputBox("Enter a value", "WSH sample",6)

The WSHSendKeys method

This method implements a SendKeys function, which simulates key strokes and send them to the window which has the focus. We use the following syntax:

obj.WSHSendKeys(keys, Wait)

The first parameter keys contains the string with the key strokes to be send. If Wait is set to true, the method waits, till the string is processed. With false the method returns immediately after sending the string.

Dim objAdr
objAdr = WScript.CreateObject("WSHExtend.WinExt")
objAdr.WSHSendKeys "ABC",true

The book discusses the details of this method. Here is a short sample:

var WSHShell = WScript.CreateObject("WScript.Shell");
var Wsh = WScript.CreateObject("WSHExtend.WinExt");
// launch Windows calculator
WSHShell.Run ("Calc.exe");

WSHWait (200);            // just wait a little bit

Wsh.WSHSendKeys ("10", true);
Wsh.WSHSendKeys ("{+}", true);
Wsh.WSHSendKeys ("2", true);
Wsh.WSHSendKeys ("=", true);

WSHWait (500);
Wsh.WSHSendKeys ("c", true);
for (var i = 1; i <=3; i++)
 {
  Wsh.WSHSendKeys (i, true);
  Wsh.WSHSendKeys ("{+}", true);
 }

WScript.Echo ("Terminate?");
WSHWait (200);
Wsh.WSHSendKeys ("%{F4}", true);  // close calc with Alt+F4
WScript.Quit();

The WSHWait method

This method suspends the script for a given time (without using the processor during this time). We use the following syntax:

obj.WSHWait(Wait)

The parameter Wait contains the delay time in Milliseconds.

Dim objAdr
objAdr = WScript.CreateObject("WSHExtend.WinExt")
objAdr.WSHWait (1000) ' Wait 1 second

The WSHWait method doesn't use any CPU time during wait, because the whole process is suspended for the delay time!

The WSHAppActivate method

This method allows to switch an already running application and its window into the foreground. We use the following syntax:

obj.WSHAppActivate(title)

The parameter must contain the ProcessID (returned from the WSHShell method) or the window title.

Dim objAdr, procID
objAdr = WScript.CreateObject("WSHExtend.WinExt")
procID = WSHShell("Calc.exe", 1) ' launch calc
WSHAppActivate(procID) ' get calculator into foreground

The WSHShell method

This method launches an application and returns the ProcessID (which is needed by the WSHAPPActivate). Use the following syntax:

obj.WSHShell(path,style)

The parameter path defines the path and name of the application to launch. The parameter style defines the window style as:

vbHide 0 Hide Window
vbNormalFocus 1 Window get focus.
vbMinimizedFocus 2 Window minimized and with focus.
vbMaximizedFocus 3 Window maximized and with focus.
vbNormalNoFocus 4 Restore Window, don't set focus.
vbMinimizedNoFocus 6 Window minimized, don't set focus.

The following code demonstrate how to use the method:

Dim objAdr, procID
objAdr = WScript.CreateObject("WSHExtend.WinExt")
procID = WSHShell("Calc.exe", 1) ' launch calc
WSHAppActivate(procID) ' Calc in foreground

The WSHBeep method

The method creates a beep massage, using the following syntax:

obj.WSHShell(mode)

The parameter Mode defines the beep mode:

0 = PC speaker
1 = MB_ICONASTERISK
2 = MB_ICONEXCLAMATION
3 = MB_ICONHAND
4 = MB_ICONQUESTION
5 = MB_OK

The following code demonstrates the use.

Dim objAdr, procID
objAdr = WScript.CreateObject("WSHExtend.WinExt")
WSHBeep (0)

We create a beep on the PC speaker.

The WSHFindWindow method

The method returns the handle to the window, using the following syntax:

obj.WSHFindWindow(WinName) = hWnd

The parameter WinName contains the window title. If the returned value is 0, no window with this title exists. Otherwise the returned value contains the handle of the window, which may be used in other methods.

The WSHFlashWindow method

The method flashes a given window or the button in the taskbar. We use the following syntax:

WSHFlashWindow(hWnd, flag) = bstatus

The parameter hWnd contains the window handle (see WSHFindWindow method). The flag specifies the window mode (flag=true highlight the title bar, flag=false show title bar normal).

The WSHMoveWindow method

Allow to move or resize a window, using the following syntax:

obj.WSHMoveWindow(hwnd, x, y, b, h) = bstatus

The parameter hWnd is the window handle (see WSHFindWindow method). The parameters x and y defines the upper left corner position of the window in pixel. In b, h we must submit the window with and height. The method returns a status code (boolean) indication either success of failure.

The WSHSetForegroundWindow method

The method switches a windows in the foreground, using the following syntax:

WSHSetForegroundWindow(hwnd) = bstatus

The parameter hWnd contains a window handle (see WSHFindWindow method). The method returns the status (boolean), indication a success (true).

The WSHExitWindows method

Allow to shutdown, logoff or restart Windows 95/98. Use the following syntax:

obj.WSHExitWindows(code) = bstatus

The parameter code defines the mode (0 = logoff, 1 = shutdown, 4 = restart). An example may be found in the book.

The WSHPlaySound method

Allows a sound output per script, using the following syntax:

WSHPlaySound(com, flag) = bstatus

The parameter com must contain the sound file ("C:\Tada.Wav"). Flag specifies the play mode (0 synchrony, or 1 asynchrony). The method returns the state (boolean) to show the success.

The WSHMciExecute method

Allows a sound output using the following syntax:

obj.WSHMciExecute(com) = bstatus

The parameter com must contain the sound file ("Play C:\Tada.wav"). The method returns the state (boolean) to show the success.

The WSHFileDialog method

Shows a file select dialog box. Use the following syntax:

WSHFileDialog(dr1) = bstatus

Submit the drive within the parameter dr1 ("C:\"), which shall be shown in the dialog box. The method returns a status code indicating whether the user has selected a file (code = true) or choose Cancel (result = false). If a file is selected, retrieve the selection using the properties DriveName, FolderName and FileName.

The WSHAbout method

Shows the about dialog box:

obj.WSHAbout

The WSHAbout1 method

Same as WSHAbout, but implemented different.

Samples using these methods are given within this book. Also the source code is described within the book. Load the WSHExtend.ocx module from the sample page of the book's website.

Terms of use

The WSHExtend ActiveX control comes AS-IS without any warranty and support. I haven't tested all methods under Windows NT, so it can be that some methods won't work under this operating system (for instance the method to exit Windows). Use the control at your own risk. In no case the author will be liable for damages or losses or whatever else resulting from the use of this component. The OCX module may be freely distributed, if this book's title is mentioned as the source for the control.

Download and install

Download the OCX file and install it on your machine. Because the ActiveX control was developed under VB, it needs the VB 5 run-time libraries (which are part of the distribution). I have created three different methods to download and install the files from my web-site http://ourworld.compuserve.com/homepages/Guenter_Born:

After installing WSHExtend you can use the WinExt object and its methods as described above.
top


by (c) Günter Born