layoutkind.explicit

.NET behaviour of LayoutKind.explicit for a field which is itself a struct

寵の児 提交于 2019-12-24 02:17:05
问题 Question I tried building a struct ( SA ) using [StructLayout(LayoutKind.Explicit)] , which had a field which is another struct ( SB ). First : I was surprised I was allowed to declare that other struct without [StructLayout(LayoutKind.Explicit)] , whereas in SA , all fields must have [FieldOffset(0)] , or the compiler will shout. It doesn't make much sense. Is this a loophole in the compiler's warnings/errors ? Second : it seems that all reference ( object ) fields in SB are moved to the

LayoutKind.Sequential not followed when substruct has LayoutKind.Explicit

*爱你&永不变心* 提交于 2019-12-18 04:41:21
问题 When running this code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace StructLayoutTest { class Program { unsafe static void Main() { Console.WriteLine(IntPtr.Size); Console.WriteLine(); Sequential s = new Sequential(); s.A = 2; s.B = 3; s.Bool = true; s.Long = 6; s.C.Int32a = 4; s.C.Int32b = 5; int* ptr = (int*)&s; Console.WriteLine(ptr[0]); Console.WriteLine(ptr[1]); Console.WriteLine(ptr[2]); Console

LayoutKind.Sequential not followed when substruct has LayoutKind.Explicit

旧巷老猫 提交于 2019-12-18 04:41:02
问题 When running this code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace StructLayoutTest { class Program { unsafe static void Main() { Console.WriteLine(IntPtr.Size); Console.WriteLine(); Sequential s = new Sequential(); s.A = 2; s.B = 3; s.Bool = true; s.Long = 6; s.C.Int32a = 4; s.C.Int32b = 5; int* ptr = (int*)&s; Console.WriteLine(ptr[0]); Console.WriteLine(ptr[1]); Console.WriteLine(ptr[2]); Console

How to convert fixed byte/char[100] to managed char[] in C#?

落爺英雄遲暮 提交于 2019-12-11 01:27:28
问题 What's the best way to convert a fixed byte or char[100] to a managed char[] in C#? I ended up having to use pointer arithmetic and I'm wondering if there is an easier way -- something like a memcpy or another way? using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace StructTest { [StructLayout(LayoutKind.Explicit)] unsafe struct OuterType { private const int BUFFER_SIZE = 100; [FieldOffset(0)] private int

Boolean Marshalling with LayoutKind.Explicit, Is this broken or failing as designed?

寵の児 提交于 2019-11-30 22:06:04
First of all the Boolean type is said to have a default marshal type of a four-byte value. So the following code works: struct A { public bool bValue1; public int iValue2; } struct B { public int iValue1; public bool bValue2; } public static void Main() { int[] rawvalues = new int[] { 2, 4 }; A a = (A)Marshal.PtrToStructure(GCHandle.Alloc(rawvalues, GCHandleType.Pinned).AddrOfPinnedObject(), typeof(A)); Assert.IsTrue(a.bValue1 == true); Assert.IsTrue(a.iValue2 == 4); B b = (B)Marshal.PtrToStructure(GCHandle.Alloc(rawvalues, GCHandleType.Pinned).AddrOfPinnedObject(), typeof(B)); Assert.IsTrue(b

Boolean Marshalling with LayoutKind.Explicit, Is this broken or failing as designed?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 05:23:04
问题 First of all the Boolean type is said to have a default marshal type of a four-byte value. So the following code works: struct A { public bool bValue1; public int iValue2; } struct B { public int iValue1; public bool bValue2; } public static void Main() { int[] rawvalues = new int[] { 2, 4 }; A a = (A)Marshal.PtrToStructure(GCHandle.Alloc(rawvalues, GCHandleType.Pinned).AddrOfPinnedObject(), typeof(A)); Assert.IsTrue(a.bValue1 == true); Assert.IsTrue(a.iValue2 == 4); B b = (B)Marshal