How to read c++ structure binary file in excel vba
I have a c++ generated structure file that contains the corresponding struct in binary form:
struct userinfo
{
char username[200];
char Password[200];
char Genkey[200];
long UniqueKey;
};
How can I read this fil开发者_开发技巧e in Excel VBA code?
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:
#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.
精彩评论