如何读取excel vba中的c ++结构二进制文件

我有一个C ++生成的结构文件,其中包含相应的二进制forms的结构:

struct userinfo { char username[200]; char Password[200]; char Genkey[200]; long UniqueKey; }; 

我如何在Excel VBA代码中读取这个文件?

这应该让你在某个地方:

 Option Explicit Type userinfo username As String * 200 Password As String * 200 Genkey As String * 200 UniqueKey As Long End Type Sub readbin() Dim rec As userinfo Dim intFileNum As Integer intFileNum = FreeFile Open "C:\Temp\my.bin" For Binary Access Read As intFileNum Do While Not EOF(intFileNum) Get intFileNum, , rec Debug.Print "->", rec.UniqueKey, rec.username Debug.Print , rec.Password Debug.Print , rec.Genkey Loop Close intFileNum End Sub 

进一步的注意事项:C ++填充不是标准化的,并且在与其他语言交互时很麻烦。 处理C ++填充的最好方法是将其删除,以使其成为语言中性。 这可以通过重新定义C ++来完成 :

 #pragma pack (push, 1) struct userinfo { char username[200]; char Password[200]; char Genkey[200]; long UniqueKey; }; #pragma pack (pop) 

虽然如果您不能修改源程序,那么您可能需要查找其他选项。