getting the size of c type in c#

  • Thread starter Thread starter SteveK
  • Start date Start date
S

SteveK

I am reading a binary file spit out from C FILE*
I would like to use Marshal.SizeOf() to get the size of the types I'm
reading, but I just realized that a C# boolean is 4 bytes versus the C 1
byte.

So, I found some snippets that I could play with and thought maybe this
would work;
int sz = Marshal.SizeOf(UnmanagedType.Bool);

This throws the unhandled exception 'System.ArgumentException'

How can I accomplish what I'm after? Any ideas?
 
Hi Steve,

In C# only bool can hold a boolean value, and UnmanagedType.Bool is very much boolean, in fact I believe it is 4 bytes too since it reflects the Win32 BOOL. Use UnmanagedType I1 with your bool.

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct S
{
[MarshalAs(UnmanagedType.I1)]
public bool b;
}
 
Back
Top