I have come up with current c# struct based on Reading binary file from delphi and Proper struct layout from delphi packed record
Now my problem is I am not getting the correct values from the file
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct test {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]
string field1;
...
}
Hers is the delphi record
char3 = Array[0..2] of Char;
test = Record
field1: char3;
...
Edit:
As I said in the comment on David's answer. The problem is that I am missing parts of the string. After following David's answer which is to use byte[]
instead of string
, my code is now working.
Just in case someone stumbled upon this delphi record to C#, which is a bit confusing since it assumes that the delphi strings are null-terminated.
Also regarding the StructLayout
Pack
attribute, just try setting this to 1 even though the Delphi record you are converting doesn't indicate it. If I am missing something regarding Pack
please correct me in the comment. Thanks
As for my final struct:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct test {
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
byte[] field1;
...
}