Skip navigation

Registry Reading and Writing Made Simple, Part 2

Using the Penton.RegObject object


In "Registry Reading and Writing Made Simple, Part 1" (October 2005, InstantDoc ID 47540), I introduced you to the Penton.RegObject object. With this object, you can easily handle Windows Management Instrumentation's (WMI's) StdRegProv class methods. As I mentioned in Part 1, you can use the Penton.RegObject object in both VBScript and JScript code, so now let's look at how you can use it to read and write registry values in both scripting languages. However, before you can use the Penton.RegObject object, you need to register the RegObject.wsc component.

Registering and Unregistering the Component
You need to register RegObject.wsc so that your scripts can create the Penton.RegObject object. The registration process adds the component's globally unique identifier (GUID) and programmatic identifier (ProgID) to the registry. You must be logged on as a member of the Administrators group to register or unregister RegObject.wsc on your system.

To register the component, right-click the RegObject.wsc file in Windows Explorer and choose Register. Alternatively, you can register the component from the command prompt. Use the command

Regsvr32 /i:"Path\RegObject.wsc" 

where Path is the path to the folder in which RegObject.wsc resides. (Although this command appears on several lines here, you would enter it on one line in the command-shell window. The same holds true for the other multiline commands in this article.)

To unregister the component, right-click the RegObject.wsc file in Windows Explorer and choose Unregister. If you want to unregister the component from the command prompt, use the command

Regsvr32 /u /n /i:"Path\RegObject.wsc"

If you want to run either command silently, specify /s as the first option on the command line after the Regsvr32 keyword (i.e., Regsvr32 /s).

Reading and Writing a Single Registry Value
You use the Penton.RegObject object's ReadValue method to read a single value from the registry, whereas you use its WriteValue method to write a single value to the registry. Because I already covered the syntax for ReadValue, WriteValue, and all the other methods and properties of the Penton.RegObject object in Part 1, I won't repeat that information here. If you have questions about the ReadValue method's parameters, see Part 1.

Listing 1 shows how to use the ReadValue method in JScript code to read the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon subkey's DefaultUserName entry from the local computer. The DefaultUserName value stores the name of the last user who logged on. When the ReadValue method succeeds, the Penton.RegObject object's Result property contains that username. The object's ValueType property contains the value's data type in string format (e.g., "REG_SZ").

In the code at callout A in Listing 1, note the use of the double backslashes (\\) in the parameter that specifies the registry subkey name. In JScript strings, the backslash character is the escape character, so you have to type it twice for it to be interpreted as a single backslash.

Listing 2 shows the WriteValue method in action. This VBScript code sets the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa subkey's RestrictAnonymous entry on the local computer. In this case, the entry is being set to 1. As documented in the Microsoft article "How to use the RestrictAnonymous registry value in Windows 2000" (, setting this registry value increases the security of a Windows system by disallowing anonymous enumeration of SAM information.

Reading and Writing REG_MULTI_SZ and REG_BINARY Values
Registry entries that contain REG_MULTI_SZ and REG_BINARY values are special. When you use the ReadValue method to read a REG_MULTI_SZ or REG_BINARY value, the Result property returns an array, so you must account for this array in your script. Take, for example, the Source entry for the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application subkey. This entry contains a REG_MULTI_SZ value that specifies a computer's event log application sources. Listing 3 demonstrates how to read this REG_MULTI_SZ value in VBScript code. As callout A in Listing 3 shows, the Result property returns an array, which is stored in the StrArray variable. The script then uses a For...Next statement to iterate through the contents of the array, displaying each member.

Listing 4 shows how to read a REG_MULTI_SZ value in JScript code. In Listing 4, note that because the Result property contains a Visual Basic (VB) safe array, the code uses JScript's toArray method to convert the VB safe array into a JScript array, as callout A shows. Also note how the code in both Listing 3 and Listing 4 checks the ValueType property to make sure that it contains a REG_MULTI_SZ value before attempting to iterate through the array.

You can apply the same techniques demonstrated in Listing 3 and Listing 4 to read REG_BINARY values. Just keep in mind that the array will contain bytes of data rather than strings.

To write a REG_MULTI_SZ or REG_BINARY value, you need to construct a VB safe array of desired values and call the WriteValue method. Listing 5 demonstrates how to write a REG_BINARY value in VBScript. Listing 6 shows the JScript equivalent. The code at callout A in Listing 6 calls the Penton.RegObject object's toVBArray method to convert the JScript array into a VB safe array before passing it to the WriteValue method.

Reading All Values in a Subkey at Once
The Penton.RegObject object's EnumValues and EnumValuesAndData methods retrieve the registry values in a subkey and store them in a Scripting.Dictionary object, which is accessible through the Penton.RegObject object's EnumDict property. As you probably know, a Scripting.Dictionary object stores items in key/value pairs. EnumValues stores the values' names and data types. EnumValuesAndData stores the values' names and contents.

Listing 7 shows an example of how to use the EnumValues method in VBScript code. This code displays the names and data types of all the values in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run subkey. To see the difference between the EnumValues and EnumValuesAndData methods, you can replace EnumValues in Listing 7 with EnumValuesAndData.

Listing 8 demonstrates how to use the EnumValues method in JScript code. As callout A in Listing 8 shows, the JScript code uses the Penton.RegObject object's dictToJSArray method to copy the Scripting.Dictionary object into a JScript array.

Simply Does It
The Penton.RegObject object addresses the WMI registry methods' shortcomings and makes reading and changing the registry both simple and elegant, no matter whether you prefer to use VBScript or JScript code. Next month, I'll present a JScript script that uses the Penton.RegObject object to manage the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run subkey on local or remote computers.

TAGS: Windows 7/8
Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.