6

windows下,安装印象笔记的时候,也安装了一个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 工具获得)(我无法发布图像,我是这里的新手......):exe 的 VBinDiff 输出

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

4

2 回答 2

12

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

    Put #iFileNum, lWritePos, vData

有了这个:

    Dim buffer() As Byte
    buffer = vData
    Put #iFileNum, lWritePos, buffer

问题解决了。

于 2012-12-18T19:40:12.717 回答
1

VB6中,在保存的内容前附加了一个神秘的额外 12 个字节。为什么?因为您保存了 Variant 结构以及 Variant 的内容。

您需要将 Variant 内容“复制”到 Byte 数组中,例如:

ReDim arrByte(0 To UBound(varBuffer) - LBound(varBuffer))

For i = 0 To UBound(varBuffer) - LBound(varBuffer)
  arrByte = varBuffer(i + LBound(varBuffer))
Next i
于 2014-06-19T15:10:25.097 回答