Use Windows Scripting Host to
Wait for a Process to End &
Perform Registry Operations

The Windows Scripting Host (WSH) lets your VB program start another application and wait for that application to terminate before it executes its next line of code. Typically you would use the WaitForSingleObject API to do this (to see how, visit my Wait for Process to Terminate page).

As show below, the RUN method of the WSH lets you start a program asynchronously or wait until it terminates depending on the last parameter. True means wait for the application to end. It also returns the error code, if any, from the shelled application.

The Windows Scripting Host also lets you easily perform basic Registry operations such as reading and writing string or numeric values and deleting single values or entire keys. No APIs involved! These operations can be performed on any part of the registry unlike VB's intrinsic SaveSettings and GetSettings commands. However, for more complex operations, such as enumerating values or keys, you will need my Registry.Bas module.

Other cool things that the WSH can be used for are: displaying a popup message box that automatically disappears after a given number of seconds, retrieve system environmental settings like the current path, DOS prompt, number of processors, current user name...

Best of all, once you have WScript.exe installed you can enter your VB script (or java script) code into a file with a .VBS extension (.js in the case of java script) and double click it to execute it. Windows associates .VBS file with Wscript.exe which will interpret and execute your code. The zip file includes several of Microsoft's sample .VBS files. Included are samples on how to create desktop shortcuts, retrieve system environmental information, read and write the registry and use the Excel object.

To use the WSH you must create a reference to the Windows Scripting Host Object Model in your project.

Wait for a Process to Terminate and Perform Registry Operations w/ Windows Scripting Host
Download Source Code

Applies To

This VB6 program uses the 6.0 version of the Common Dialog Control and will not run under VB5 unless you also have VB6 installed.

Wshom.ocx began shipping with Windows 98 but was available as an add on for Windows 95 and NT 4.0. You can download the Windows Script engine from Microsoft and it will work with Windows 9x, NT and Windows 2000. It also installs Wscript.exe which allows you to create very powerful batch files as discussed above. Visit the Scripting home page on Microsoft's site.

Waiting for an Application to Terminate

To start an application and have control return immediately:

Create an instance of the WSH and call its .Run method. .Run takes three parameters. The first is the path of the application to run. The second is a flag indicating how to display the application's window. Here I use normal. Other choices are minimized and maximized. The last parameter is set to False indicating that control should return immediately to the oShl.Popup line of code.

The .Popup method is used to display a message box and resembles the MessageBox command very closely. The main difference is that you can specify the number of seconds, 4 in this case, to display the message box for.

   Dim oShl As New IWshShell_Class
   Call oShl.Run(txtTarget, SW_SHOWNORMAL, False)
   Call oShl.Popup("Application was run asynchronously", 4, "WSH Example", vbInformation)

To start an application and wait until it terminates:

The code to wait for the shelled application to finish is identical to that above except that the last parameter passed to the .Run method is set to True instead of False.

Registry Operations

Reading and writing the registry is easy with the WSH. Windows Scripting Host provides a .RegRead, .RegWrite and .RegDelete method to help you accomplish basic operations.

.RegWrite can write string, integer (Reg_Dword) and limited binary values to new or existing registry locations. If the location does not exists it will be created. Similarly, .RegRead can read the same types of values. To delete a registry key or value, you can use the .RegDelete method. See the source code for further details.


Waiting for a process to terminate:

Press F5 to run the program. Enter or browse for the application to run. Click the desired option and then the "Start It" button. You will see a message box immediately if you ran it asynchronously or when you close the shelled program it you chose to wait for it.

Registry Operations:

This sample writes, reads then delete three registry values. Step through the code pressing the F8 key. After each operation, look at the indicated location in the registry using RegEdit to see the results. You may need to refresh the registry's display by pressing F5 in RegEdit after each statement is executed.

System Settings:

This sample extracts a few environmental variables such as the path, prompt, Window's directory,... Step through the code pressing the F8 key. Make sure the Immediate windows is open to see the output of the debug.print commands.

About TheScarms
About TheScarms

Sample code
version info

If you use this code, please mention ""

Email this page

© Copyright 2016 TheScarms
Goto top of page