我有一个二进制文件,并希望从该文件的特定位置读取值。一旦我有了这个值,我想要将这个值写入位于同一位置的另一个文件中。
例如,我有两个二进制文件A1和B1,现在我想从文件A1中读取一个值,该文件存储在二进制位置0000 003E-0000 0041中。
一旦我有了这个值,就必须在文件B1的相同位置上写入值。
它是 VB6 而不是VBA,我不知道如何从VB6中的特定字节位置读取和写入字节
如果我能在VB6代码中获得适当的解决方案,我将不胜感激。
发布于 2022-11-05 08:07:15
将此模块添加到一个新的Std项目中,并在以下代码中更改与CopyChunk函数一起使用的源文件名和目标文件名:
Option Explicit
Private Sub Form_Load()
Dim sError As String
If Not CopyChunk("D:\TEMP\aaa.src", "D:\TEMP\aaa.dest", _
Position:=&H3E, _
Size:=&H41 - &H3E + 1, _
Error:=sError) Then
MsgBox "Error copying chunk" & vbCrLf & vbCrLf & sError, vbExclamation
End If
End Sub
Private Function CopyChunk( _
SourceFile As String, _
DestFile As String, _
ByVal Position As Currency, _
ByVal Size As Long, _
Optional Error As String) As Boolean
Dim pSrc As stdole.IUnknown
Dim pDest As stdole.IUnknown
Dim baData() As Byte
On Error GoTo EH
Set pSrc = StreamOpenFile(SourceFile)
If pSrc Is Nothing Then
Error = "Cannot open source file (" & SourceFile & ")"
GoTo QH
End If
Set pDest = StreamOpenFile(DestFile, AlwaysCreate:=False)
If pDest Is Nothing Then
Error = "Cannot open destination file (" & DestFile & ")"
GoTo QH
End If
If StreamSeekAbsolute(pSrc, Position) <> Position Then
Error = "Cannot position source file to " & Position
GoTo QH
End If
If StreamSeekAbsolute(pDest, Position) <> Position Then
Error = "Cannot position destination file to " & Position
GoTo QH
End If
baData = StreamReadBytes(pSrc, Size)
If UBound(baData) + 1 <> Size Then
Error = "Cannot read chunk from source file (" & Size & ")"
GoTo QH
End If
If StreamWriteBytes(pDest, baData) <> Size Then
Error = "Cannot write chunk to destination file (" & Size & ")"
GoTo QH
End If
'--- success
CopyChunk = True
QH:
Exit Function
EH:
Error = Err.Description
End Functionhttps://stackoverflow.com/questions/74221655
复制相似问题