views:

2267

answers:

3

If I have two variables containing binary values, how do I append them together as one binary value? For example, if I used WMI to read the registry of two REG_BINARY value, I then want to be able to concatenate the values.

VBScript complains of a type mismatch when you try to join with the '&' operator.

A: 

Perhaps...

result = CStr(val1) & CStr(val2)
aphoria
+1  A: 

REG_BINARY value will be returned as an array of bytes. VBScript may reference an array of bytes in a variable and it may pass this array of bytes either as a variant to another function or as a reference to array of bytes. However VBScript itself can do nothing with the array.

You are going to need some other component to do some from of concatenation:-

Function ConcatByteArrays(ra, rb)
 Dim oStream : Set oStream = CreateObject("ADODB.Stream")
 oStream.Open
 oStream.Type = 1 'Binary'
 oStream.Write ra
 oStream.Write rb

 oStream.Position = 0

 ConcatByteArrays = oStream.Read(LenB(ra) + LenB(rb))
 oStream.Close

End Function

In the above code I'm using the ADODB.Stream object which is ubiquitous on currently supported platforms.

If you actually had multiple arrays that you want to concatenate then you could use the following class:-

Class ByteArrayBuilder
 Private moStream

 Sub Class_Initialize()
  Set moStream = CreateObject("ADODB.Stream")
  moStream.Open
  moStream.Type = 1
 End Sub

 Public Sub Append(rabyt)
  moStream.Write rabyt
 End Sub

 Public Property Get Length
  Length = moStream.Size
 End Property

 Public Function GetArray()
  moStream.Position = 0
  GetArray = moStream.Read(moStream.Size)
 End Function

 Sub Class_Terminate()
  moStream.Close
 End Sub

End Class

Call append as many times as you have arrays and retrieve the resulting array with GetArray.

AnthonyWJones
+1  A: 

For the record, I wanted VBScript code for a large userbase as a logon script that has the least chance of failing. I like the ADO objects, but there are so many mysterious ways ADO can be broken, so I shy away from ADODB.Stream.

Instead, I was able to write conversion code to convert binary to hex encoded strings. Then, to write back to a REG_BINARY value, I convert it to an array of integers and give it to the SetBinaryValue WMI method.

Note: WshShell can only handle REG_BINARY values containing 4 bytes, so it's unusable.

Thank you for the feedback.

spoulson