MOTOBIT.COM - ASP upload, IIS utility VBS and binary values Article

Member of  RegEdit.Value | Changes | Purchase | Download

VBS and binary values

VBS does not have functions to work with native binary data (VT_UI1 | VT_ARRAY). We can use multibyte string functions to handle binary registry values in VBS.

Convertion between binary and string data.

      You can use simple PureVBS functions to convert between string (unicode, native VBS type) and multibyte string data types:
'Converts binary data or multibyte string to a unicode string
Function BinaryToStringSimple(Binary)
  Dim I, S
  For I = 1 To LenB(Binary)
    S = S & Chr(AscB(MidB(Binary, I, 1)))
  BinaryToStringSimple = S
End Function

'Converts unicode string to a multibyte string
Function StringToMB(S)
  Dim I, B
  For I = 1 To Len(S)
    B = B & ChrB(Asc(Mid(S, I, 1)))
  StringToMB = B
End Function

'Converts array of numbers to a multibyte string
Function ArrayToMB(A)
  Dim I, MB
  For I = LBound(A) To UBound(A)
    MB = MB & ChrB(A(I))
  ArrayToMB = MB
End Function

Store binary registry value

      The shortest way (and pure VBS) is to use String-to-Multibyte conversion function and Setvalue method with vtBinary data type:
Sub SetBinaryValue()
  Dim S, MyKey
  'Get RegEdit.Server object
  Set S = CreateObject("RegEdit.Server")
  'Add a new key
  Set MyKey = S.GetKey("HKEY_LOCAL_MACHINE\SOFTWARE").CreateKey("MyFirstKey")
  'Set binary value - zero terminated binary string
  MyKey.Values("BinaryData").SetValue StringToMB("Some binary data") & ChrB(0), vtBinary
End Sub
You can set any binary value using ArrayToMB conversion function:
  MyKey.Values("BinaryData").SetValue ArrayToMB(Array(7, 56, 255, 54, 78, 94, 0, 44)), vtBinary

Read binary registry value

      We can read binary value and store it to a variable in VBS as in VBA. The only way to work with such value in VBS are multibyte string functions - LeftB, MidB, InstrB, ChrB, LenB. You can use some external object to handle binary data - ADODB.Stream, ADODB.Recordset or our ScriptUtils.ByteArray.
      BinaryToStringSimple uses this function to convert binary data to a string :
Sub ReadBinaryValue()
  Dim S, MyKey
  'Get RegEdit.Server object
  Set S = CreateObject("RegEdit.Server")
  'Add a new key
  'Restore binary value
  Dim Value: Value = MyKey.Values("BinaryData").Value
  'Value variable contains VT_UI1 | VT_ARRAY - binary data.
  Wscript.Echo BinaryToStringSimple(Value)
End Sub
      You can get any of the source byte from binary data using these functions
'Get first byte
FirstByte = AscB(MidB(Value, 1, 1))
'Get byte no. 7
Byte7 = AscB(MidB(Value, 7, 1))

'Get all bytes
Dim I: For I = 1 To LenB(Value)
  Wscript.Echo AscB(MidB(Value, I, 1))

Other links for VBS and binary values article


Intuitive, easy to use COM interface to windows registry. Set of classes to read/enumerate/modify windows registry keys and values from ASP, VBS and T-SQL.

© 1996 - 2009 Antonin Foller, Motobit Software | About, Contacts | e-mail: