首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从二进制文件中读取字节并将该值写入VB6中的另一个二进制文件

从二进制文件中读取字节并将该值写入VB6中的另一个二进制文件
EN

Stack Overflow用户
提问于 2022-10-27 11:56:11
回答 1查看 75关注 0票数 1

我有一个二进制文件,并希望从该文件的特定位置读取值。一旦我有了这个值,我想要将这个值写入位于同一位置的另一个文件中。

例如,我有两个二进制文件A1和B1,现在我想从文件A1中读取一个值,该文件存储在二进制位置0000 003E-0000 0041中。

一旦我有了这个值,就必须在文件B1的相同位置上写入值。

它是 VB6 而不是VBA,我不知道如何从VB6中的特定字节位置读取和写入字节

如果我能在VB6代码中获得适当的解决方案,我将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2022-11-05 08:07:15

将此模块添加到一个新的Std项目中,并在以下代码中更改与CopyChunk函数一起使用的源文件名和目标文件名:

代码语言:javascript
复制
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 Function
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74221655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档