简体   繁体   中英

Not getting correct number of characters in Marshal.PtrToStructure

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;
  ...
}

The correctness of the code in the question depends entirely on what Char is.

  • In Delphi 2009 and later, Char is an alias to WideChar which is a 16 bit type.
  • In earlier versions, Char is an alias to AnsiChar which is a 8 bit type.

So, your code is correct if you are using a version of Delphi earlier than Delphi 2009. Otherwise your C# code should replace CharSet.Ansi with CharSet.Unicode .

The other difference is that the C# code insists on the array being null-terminated, whereas the Delphi code need not. So if your Delphi code wants to use all three elements as non-null values then you would need to avoid using a string on the C# side. It would become:

[StructLayout(LayoutKind.Sequential)]
struct test {
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  byte[] field1;
  ...
}

for pre-Unicode Delphi, or

[StructLayout(LayoutKind.Sequential)]
struct test {
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  char[] field1;
  ...
}

for Unicode Delphi.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM