如何读取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)
虽然如果您不能修改源程序,那么您可能需要查找其他选项。
- 如何拦截内置的Officefunction区控件的点击
- ASP.NET WebApi无法在Azure中使用COM(Excel)
- 如何解决这个转换器hex到八进制像excel?
- 有没有可能使用Excel JavaScript API编程调用excel add方法?
- 使用OpenXML访问Excel行中的单元格值 – 未将对象引用设置为对象的实例
- C#:closuresExcel窗口closures事件
- Excel VBA AllocConsole:在控制台中select的文本之后,Dll例程冻结
- 使用Microsoft.Office.Interop.Excel另存为错误导出到.xlsx
- C#Excel 2007错误