2011-06-23 167 views
3

C++結構二進制文件我有一個C++生成的結構文件,其中包含以二進制形式對應的結構:如何讀取Excel VBA中

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

我如何讀取Excel中的VBA代碼此文件?

回答

2

這應該讓你的地方:

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++填充的最好方法是將其刪除,以使其成爲語言中性。這可以通過redefining C++ to做到:

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

但如果你不能修改源程序,那麼你可能需要看看周圍其他的選擇。

+1

結構填充將使這不可靠。 – Arafangion

+0

關於如何解開C++填充規則的提示?在什麼記錄*不是* 604字節? –