Registry Reading and Writing Made Simple, Part 1

Introducing the Penton.RegObject object

Bill Stewart

September 12, 2005

15 Min Read
ITPro Today logo


The introduction of Windows Management Instrumentation (WMI) as a core OS component in Windows 2000 made many administrative scripting tasks far easier, but Microsoft has recognized that registry management still plays an important role in Windows management. The company has provided several tools for reading from and writing to the registry in a script. These tools include:

*Registry import (.reg) files. You can modify the registry by using a .reg file and the Regedit /s command, but this method lets you only update the registry. You can't read data from the registry, and there's no error checking. Also, a .reg file can update the registry only on a local computer, not a remote computer.

*The Reg.exe command-line utility. You can use the Reg.exe command-line utility, which is part of the OS in Windows XP and part of the resource kits for Win2K and Windows NT, to update the registry. You can also use Reg.exe to read data from the registry. However, to use the output, you have to parse it, which is an error-prone process.

*The WshShell object methods. Windows Script Host's (WSH's) WshShell object provides a set of methods for reading from and writing to the registry, but these methods suffer from limitations. As with .reg files, you can read from and write to the registry only on the local computer. Also, there are limits to the types of data you can write. For example, you can't write a REG_MULTI_SZ value or more than 32 bits of data in a REG_BINARY value.

Although all three tools are useful in various scenarios, Microsoft has provided a more flexible tool to access the registry programmatically: WMI's StdRegProv class. This class provides a set of methods for reading from and writing to the registry. However, despite their flexibility, the StdRegProv methods are cumbersome to use and have three main limitations:

*The registry hive must be specified numerically. For example, Instead of writing HKEY_LOCAL_MACHINE or HKLM, you have to use the hexadecimal value 80000002. Although you can use variables or constants in a script to represent these values, it would be simpler and more convenient if the StdRegProv class were able to accept the registry hive name as a string.

*There are separate methods for each data type. The StdRegProv class methods let you read and write all the commonly used registry value types, but unfortunately, you have to use a separate method for each type. For example, to read a REG_MULTI_SZ value, you have to call the GetMultiStringValue method. This limitation is bothersome when you want to read a registry value regardless of its type or if you don't know the type beforehand.

*Accessing the methods from JScript is difficult. JScript's language design dictates that primitive data types (i.e., numeric and Boolean values) are always passed by value. There's no equivalent of VBScript's ByRef keyword in a JScript function declaration that lets you pass a primitive data item by reference. ByRef parameters are used in several of the StdRegProv methods. Although you can use the SWbemMethod object and related objects to call these methods, you can't simply call the methods directly as you can in VBScript. Also, the StdRegProv methods use VBArray objects, which are Visual Basic (VB) safe arrays--and there's no built-in way to convert a JScript array to a VBArray object.

Because of these limitations, I decided to write RegObject.wsc, a Windows Script Components (WSC) component to handle the WMI StdRegProv methods. You can download RegObject.wsc from the Windows Scripting Solutions Web site. Go to http://www.windowsitpro.com/windowsscripting, enter 47540 in the InstantDoc ID text box, then click the 47540.zip hotlink. To use RegObject.wsc, you need to know about the Penton.RegObject object and its methods.

Introducing the VersatilePenton.RegObject Object

The Penton.RegObject object is implemented in the RegObject.wsc file. A .wsc file is an XML-formatted file containing an ActiveX object implemented using script code. One advantage to using WSC is that you can implement the object in multiple languages. RegObject.wsc uses both VBScript and JScript to do its work.

A .wsc file contains four main XML elements: , , , and

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like