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.
'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))) Next 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))) Next 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)) Next ArrayToMB = MB End Function
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 SubYou can set any binary value using ArrayToMB conversion function:
MyKey.Values("BinaryData").SetValue ArrayToMB(Array(7, 56, 255, 54, 78, 94, 0, 44)), vtBinary
Sub ReadBinaryValue() Dim S, MyKey 'Get RegEdit.Server object Set S = CreateObject("RegEdit.Server") 'Add a new key Set MyKey = S.GetKey("HKEY_LOCAL_MACHINE\SOFTWARE\MyFirstKey") 'Restore binary value Dim Value: Value = MyKey.Values("BinaryData").Value 'Value variable contains VT_UI1 | VT_ARRAY - binary data. Wscript.Echo BinaryToStringSimple(Value) End SubYou 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)) Next
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: info@pstruh.cz