For Programmers: Free Programming Magazines  


Home > Archive > WSH > February 2006 > Using ADO.Stream to write binary "array"?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using ADO.Stream to write binary "array"?
news@mail.adsl4less.com

2006-02-02, 6:55 pm

I create my stream object as per the thousands of examples on the net:

Set stream=Server.CreateObject("ADODB.Stream")
stream.Open
stream.Type=1
stream.Write bytearray

None of them seem to explain where bytearray comes from or put it into
wider context. What I'm trying to write to the stream is actually
REG_BINARY data which was read using WMI like this:

objReg.GetBinaryValue HKEY_CURRENT_USER, KeyPath, ValueName, bytearray

According to the documentation, GetBinaryValue gives me "array of
binary bytes", but this "array of binary bytes" isn't liked by
stream.write as it gives me the error:

"ADODB.Stream: Arguments are of the wrong type, are out of acceptable
range, or are in conflict with one another."

I can loop thought the bytearray returned by GetBinaryValue with a for
next to prove there's something in there, and it certainly looks like a
byte array to me, so any ideas where I'm going wrong? Does the
bytearray that the ADO.Stream expects to receive have some special
quality that I'm missing?

TIA
Mark

Miyahn

2006-02-02, 6:55 pm

<news@mail.adsl4less.com> wrote in message news:1138906243.054676.189610@g14g2000cwa.googlegroups.com
> I create my stream object as per the thousands of examples on the net:
>
> Set stream=Server.CreateObject("ADODB.Stream")
> stream.Open
> stream.Type=1
> stream.Write bytearray
>
> None of them seem to explain where bytearray comes from or put it into
> wider context. What I'm trying to write to the stream is actually
> REG_BINARY data which was read using WMI like this:
>
> objReg.GetBinaryValue HKEY_CURRENT_USER, KeyPath, ValueName, bytearray
>
> According to the documentation, GetBinaryValue gives me "array of
> binary bytes", but this "array of binary bytes" isn't liked by
> stream.write as it gives me the error:
>
> "ADODB.Stream: Arguments are of the wrong type, are out of acceptable
> range, or are in conflict with one another."


I use the following sub procedure to wright byte array using ADODB.Stream.

Sub WriteBinary(FileName, Buf)
Dim I, aBuf, Size, bStream
Size = UBound(Buf): ReDim aBuf(Size \ 2)
For I = 0 To Size - 1 Step 2
aBuf(I \ 2) = ChrW(Buf(I + 1) * 256 + Buf(I))
Next
If I = Size Then aBuf(I \ 2) = ChrW(Buf(I))
aBuf=Join(aBuf, "")
Set bStream = CreateObject("ADODB.Stream")
bStream.Type = 1: bStream.Open
With CreateObject("ADODB.Stream")
.Type = 2 : .Open: .WriteText aBuf
.Position = 2: .CopyTo bStream: .Close
End With
bStream.SaveToFile FileName, 2: bStream.Close
Set bStream = Nothing
End Sub

--
Miyahn (Masataka Miyashita) JPN
Microsoft MVP for Microsoft Office - Excel(Jan 2006 - Dec 2006)
HQF03250@nifty.ne.jp

news@mail.adsl4less.com

2006-02-08, 8:53 am

Many thanks.

If I understand correctly, what's your saying is that ADODB.Stream only
accepts a byte array of type Byte(), whereas I'm trying to send it an
array of type Variant()? If so, the only way I can create a genuine
byte array in vbscript is to use, ironically, ADODB.Stream. Problem is,
the binary version, as we know, expects a Byte()! However, we can use
the text version of ADODB.Stream and pass our array of Variants to it.
Then we copy the contents of the text stream directly into the binary
stream. Phew - what a lot of effort for something so simple!

I this case, if I understand correctly, you're taking advantage of the
fact that REG_BINARY stores the data in 2-byte unicode. However, what
if I'm dealing in geniune binary data? Can I still use this method (as
in, will ChrW be happy to output data that it not part of any unicode
character set)?

Miyahn

2006-02-08, 8:53 am

<news@mail.adsl4less.com> wrote in message news:1138964175.606697.174440@g47g2000cwa.googlegroups.com
> Many thanks.
>
> If I understand correctly, what's your saying is that ADODB.Stream only
> accepts a byte array of type Byte(), whereas I'm trying to send it an
> array of type Variant()?


Yes.

> I this case, if I understand correctly, you're taking advantage of the
> fact that REG_BINARY stores the data in 2-byte unicode. However, what
> if I'm dealing in geniune binary data? Can I still use this method (as
> in, will ChrW be happy to output data that it not part of any unicode
> character set)?


Yes you can.

Drag and drop any binary file to the following script.
The script simply read a file to an array, and write back the array to
the same file.
(Make sure to prepare backup file before trying.)

' FileName : BinIOExp.vbs
Dim Buffer, FileName
FileName = WScript.Arguments(0)
Buffer = ReadBinary(FileName)
WriteBinary FileName, Buffer
MsgBox "Test was completed"
'
Function ReadBinary(FileName)
Dim Buf(), I
With CreateObject("ADODB.Stream")
.Mode = 3: .Type = 1: .Open: .LoadFromFile FileName
ReDim Buf(.Size - 1)
For I = 0 To .Size - 1: Buf(I) = AscB(.Read(1)): Next
.Close
End With
ReadBinary = Buf
End Function
'
Sub WriteBinary(FileName, Buf)
Dim I, aBuf, Size, bStream
Size = UBound(Buf): ReDim aBuf(Size \ 2)
For I = 0 To Size - 1 Step 2
aBuf(I \ 2) = ChrW(Buf(I + 1) * 256 + Buf(I))
Next
If I = Size Then aBuf(I \ 2) = ChrW(Buf(I))
aBuf=Join(aBuf, "")
Set bStream = CreateObject("ADODB.Stream")
bStream.Type = 1: bStream.Open
With CreateObject("ADODB.Stream")
.Type = 2 : .Open: .WriteText aBuf
.Position = 2: .CopyTo bStream: .Close
End With
bStream.SaveToFile FileName, 2: bStream.Close
Set bStream = Nothing
End Sub

--
Miyahn (Masataka Miyashita) JPN
Microsoft MVP for Microsoft Office - Excel(Jan 2006 - Dec 2006)
HQF03250@nifty.ne.jp

news@mail.adsl4less.com

2006-02-08, 8:53 am

Excellent. Thank you very much.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com