How Can I Write Binary Data to the Registry?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I write binary data to the registry?

— FG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, FG. WMI’s Standard Registry Provider includes a method – SetBinaryData – that makes it relatively easy to write binary data to the registry. There’s one little catch to be aware of, but we’ll let you know what that is.

First, though, let’s take a look at a script that writes the numbers 1 through 10 to a binary registry value named BinaryTest found in HKCU\Software. If the referenced registry value exists then the script will simply replace the existing value with the new value (in this case, the numbers 1 though 10). If the value doesn’t exist, the script will create BinaryTest and then assign it the numbers 1 through 10. In other words, you don’t need to create BinaryTest before trying this sample script; if BinaryTest doesn’t exist, WMI will create it for you.

Here’s the script:

Const HKEY_CURRENT_USER = &H80000001

strComputer = “.”

Set objRegistry = GetObject _ (“winmgmts:\\” & strComputer & “\root\default:StdRegProv”)

strKeyPath = “Software” strValueName = “BinaryTest” arrValues = Array(1,2,3,4,5,6,7,8,9,10)

errReturn = objRegistry.SetBinaryValue _ (HKEY_CURRENT_USER, strKeyPath, strValueName, arrValues)

The script begins by defining a constant named HKEY_CURRENT_USER and assigning it the value &H80000001; this tells the Standard Registry Provider that we want to work with the HKEY_CURRENT_USER portion of the registry. We then connect to the WMI service; note that the Standard Registry Provider is found in the root\default namespace. That’s different from the vast majority of WMI classes used for system administration, most of which are found in root\cimv2.

We assign the key name Software to a variable named strKeyPath; this is the registry path within HKCU. A second variable – strValueName – is assigned the value BinaryTest, which happens to be the registry value we want to change. Finally we use this line of code to assign the numbers 1 through 10 to an array variable we named arrValues; this, of course, represents the binary data we want to write to the registry:

arrValues = Array(1,2,3,4,5,6,7,8,9,10)

Having defined our constant and assigned values to all our variables, all that’s left is to call the SetBinaryValue metod to write these 10 values to the registry:

errReturn = objRegistry.SetBinaryValue _
    (HKEY_CURRENT_USER, strKeyPath, strValueName, arrValues)

So what’s the catch? Well, if you’re familiar with binary values in the registry, you know that numbers are represented by hexadecimal values. You don’t see the number 1 in a binary registry value; you see 01. Likewise you don’t see the number 10 in a binary registry value; you see 0A. The registry works with hexadecimal values, but the SetBinaryValue method requires us to pass regular decimal numbers. That can be a little bit confusing if you’re following instructions for setting a registry value and the instructions say, “Set the value to 0A.” Before you can use the SetBinaryValue method you’ll need to convert 0A to its decimal equivalent. (An easy way to do that? Use Calculator in Scientific mode.)

Other than that, though, setting a binary value in the registry is no different – and no more difficult – than setting a string value or a numeric value.


Discussion is closed.

Feedback usabilla icon