gpt4 book ai didi

vba - 将字节数组写入文件的问题

转载 作者:行者123 更新时间:2023-12-04 10:27:48 26 4
gpt4 key购买 nike

windows下,当Evernote安装了,也安装了一个api,可以通过vba访问(例如)。

每个笔记都可以显示其“资源”(附加文件和图像),并且可以将实际资源作为字节数组检索。

我无法将字节数组写入实际文件。

变量声明:

Dim fileByte() As Byte
Dim nt As enapiLib.Note

获取数据:
fileByte = nt.Resources.Item(i).Data

将字节数组写入文件:
Function WriteByteArray(vData As Variant, sFileName As String, Optional bAppendToFile As Boolean = False) As Boolean
Dim iFileNum As Integer, lWritePos As Long

Debug.Print " --> Entering WriteByteArray function with " & sFileName & " file to write."
On Error GoTo ErrFailed
If bAppendToFile = False Then
If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then
'Delete the existing file
VBA.Kill sFileName
End If
End If

iFileNum = FreeFile
Debug.Print "iFileNum = " & iFileNum
'Open sFileName For Binary Access Write As #iFileNum
Open sFileName For Binary Lock Read Write As #iFileNum

If bAppendToFile = False Then
'Write to first byte
lWritePos = 1
Else
'Write to last byte + 1
lWritePos = LOF(iFileNum) + 1
End If

Put #iFileNum, lWritePos, vData
Close #iFileNum

WriteByteArray = True
Exit Function

ErrFailed:
Debug.Print "################################"
Debug.Print "Error handling of WriteByteArray"
Debug.Print "################################"
FileWriteBinary = False
Close iFileNum
Debug.Print Err.Description & "(" & Err.Number & ")"
End Function

我尝试了一个exe文件

通过 debug.printing 每个字节值,我知道我的字节数组以 4D 5A 开头,就像其他每个 exe 文件一样
Resource (1) : 
ClickToSetup.0.9.8.1416.exe
application/x-msdownload
Le fichier C:\Dropbox\TestEvernote\ClickToSetup.0.9.8.1416.exe doit être créé.
Lbound(fileByte) = 0
Ubound(fileByte) = 5551919
i = 0
filebyte(i) = 4D
i = 1
filebyte(i) = 5A

通过将创建的 exe 文件读回字节数组,我知道新创建的数组以字节 4D 5A 开头,如所愿

但是硬盘驱动器上的 exe 文件是_损坏_,并且_没有以正确的字节_开始_:

以下是硬盘驱动器上存储文件的第一个二进制值:(来自 VBinDiff 工具)(我无法发布图像,我是这里的新手...): VBinDiff output of exe

为什么实际数据前面有这12个字节?

最佳答案

我遇到了同样的问题 - 在每个写入的文件的顶部都会抛出一些 12 字节的标题。事实证明,PUT 命令并不十分清楚如何处理 Variant 类型的数据。我不确定确切的原因,但我的解决方法是简单地替换 PUT 行:

    Put #iFileNum, lWritePos, vData

有了这个:
    Dim buffer() As Byte
buffer = vData
Put #iFileNum, lWritePos, buffer

问题解决了。

关于vba - 将字节数组写入文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12499017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com