ADODB.Stream : Replacing One Byte w/Another

From: Scott McNair (scott.mcnair_at_sfmco.takethispartout.com)
Date: 04/28/04


Date: Wed, 28 Apr 2004 14:53:53 -0700

Hi,

I'm trying to open a binary file, change the value of certain byte
fields, and write the data back. It sounds easy, but I've been fighting
with it most of the afternoon without luck. I've tried several things,
but nothing works... the latest involves creating a scratch stream
object to hold temporary data, passing from the original stream until I
get to the position I want to change, and then inserting the two bytes I
want to replace, then filling it out to the end. It bombs at around the
Chr(SecondHex) line.

[FilePos: The byte-position I'm replacing in the new file]
[FirstHex & SecondHex: ASCII equivalents of the data I want to put in]
[ROMStream: The original stream]

Dim ScratchStream
Set ScratchStream = CreateObject("ADODB.Stream")
ScratchStream.Mode = adModeReadWrite
ScratchStream.Type = adTypeBinary
ScratchStream.Open
ROMStream.Position = 0
ScratchStream.Write ROMStream.Read(FilePos-1)
ROMStream.Position = FilePos + 1
ScratchStream.Write Chr(SecondHex)
ScratchStream.Write Chr(FirstHex)
ScratchStream.Write ROMStream.Read()
ScratchStream.CopyTo ROMStream
ScratchStream.Close

Is there a way to just replace a particular byte in a stream with your
own data? According to what I've read, Stream.Write does not replace
but instead inserts... all I want to do is replace.

Thanks,
Scott