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

时间:2022-09-02 08:42:11

I have a c++ generated structure file that contains the corresponding struct in binary form:

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

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

How can I read this file in Excel VBA code?

如何在Excel VBA代码中读取此文件?

1 个解决方案

#1


2  

This should get you somewhere:

这应该会让你到处:

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

Further notes: C++ padding is not standardized, and is a pain when interfacing with other languages. The best way to deal with C++ padding is to remove it so it to become language neutral. This can be done by redefining C++ to:

进一步说明:C ++填充不是标准化的,在与其他语言交互时很痛苦。处理C ++填充的最佳方法是删除它,使其成为语言中立的。这可以通过将C ++重新定义为:

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

Although if you can't modify the source program, then you might need to look around for other options.

虽然如果您无法修改源程序,那么您可能需要四处寻找其他选项。

#1


2  

This should get you somewhere:

这应该会让你到处:

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

Further notes: C++ padding is not standardized, and is a pain when interfacing with other languages. The best way to deal with C++ padding is to remove it so it to become language neutral. This can be done by redefining C++ to:

进一步说明:C ++填充不是标准化的,在与其他语言交互时很痛苦。处理C ++填充的最佳方法是删除它,使其成为语言中立的。这可以通过将C ++重新定义为:

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

Although if you can't modify the source program, then you might need to look around for other options.

虽然如果您无法修改源程序,那么您可能需要四处寻找其他选项。