Trying to call DeviceIoControl function from managed code. Have couple structures defined like this:
[StructLayout(LayoutKind.Explicit)]
public struct PARTITION_INFORMATION_UNION
{
[FieldOffset(0)]
public PARTITION_INFORMATION_MBR Mbr;
[FieldOffset(0)]
public PARTITION_INFORMATION_GPT Gpt;
}
[StructLayout(LayoutKind.Sequential)]
public struct PARTITION_INFORMATION_MBR
{
public byte PartitionType;
public bool BootIndicator;
public bool RecognizedPartition;
public UInt32 HiddenSectors;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct PARTITION_INFORMATION_GPT
{
public Guid PartitionType;
public Guid PartitionId;
[MarshalAs(UnmanagedType.U8)]
public UInt64 Attributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 36)]
public String Name;
}
Works fine if I build my application in x86, but fails during run-time if built for x64 with the following error message:
Unhandled Exception: System.TypeLoadException: Could not load type PARTITION_INFORMATION_UNION' from assembly 'Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.
Any idea what am I doing wrong?
Update: as far as I understand problem is with last string defined in PARTITION_INFORMATION_GPT. If I replace it with 9 8-byte numbers, or simple comment out and put hardcoded Size into structure attributes - everything works fine.