structlayout

Marshal.Sizeof() returning unexpected value

夙愿已清 提交于 2021-01-27 12:34:55
问题 I'm debugging code in C# written by a 3rd party. The project is an old C++ project that was rewritten in C# by a contractor, and I have no access to the contractor. I authored the original C++ version. The issue is when the C# code gets the size of a structure that represents data received over a UDP connection. The struct is defined as: [StructLayout(LayoutKind.Sequential,Pack=1)] internal class PROXY_HDR { public ushort pad; public ushort label; public char flags; public ushort length;

C# StructLayout Pack get System.NullReferenceException, alignement issue?

非 Y 不嫁゛ 提交于 2019-12-25 08:28:09
问题 First of all, I would say that, I'm not stuck, as there is a lot of workaround for me !! The best of then is that I don't need 1Byte Pack anymore :P But honestly, I'm quite puzzle about the behavior. I'm using Unity, It works perfectly on Windows but failed on Android(mono) Have I write something invalid ? Do I have rise a bug on Mono ?! Comments are definitivly welcome !! public class TEST { [StructLayout(LayoutKind.Sequential, Pack = 1)] private struct MyStruct { public float field1; public

Union in c# with StructLayout

人走茶凉 提交于 2019-12-23 19:45:50
问题 I have multiple structs that all starts with a header struct. Like this public struct BaseProtocol { public Header header; public Footer footer; }; The header is public struct Header { public Byte start; public Byte group; public Byte dest; public Byte source; public Byte code; public Byte status; }; The problem now is that I need to union them with a Byte[]. I tried it with this [StructLayout( LayoutKind.Explicit, Size=255 )] public struct RecBuffer { [FieldOffset( 0 )] public Header header;

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

Modify struct layout from p/invoke

耗尽温柔 提交于 2019-12-08 00:03:57
问题 I'm looking for best practice guidance around changing the struct/class layout of objects returned/passed into a p/invoke function. I've searched for an answer to this but maybe I'm just too tired and I'm not searching effectively. The simplest example I can come up with (the real one is a bit too complex for here) is with something like GetWindowRect. If I wanted to add a few extra properties to the RECT struct, should I just add it to the definition for the struct itself or should I switch

undefined behaviour with StructLayout and FieldOffset

非 Y 不嫁゛ 提交于 2019-12-02 22:51:04
问题 I have extracted with success a structure object from a valid pointer using the PtrToStructure function (in VB.NET), but the result of some object members are not correct (comparing with the C++ example): the code is : StructLayout(LayoutKind.Explicit, pack:=1, CharSet:=CharSet.Ansi)> _ Public Structure MyStruct <FieldOffset(0)> _ Dim Width As UInt32 ' 350 correct <FieldOffset(4)> _ Dim Height As UInt32 ' 466 correct <FieldOffset(20)> _ Dim Buffer As IntPtr ' variable but correct <FieldOffset

Why does the System.DateTime struct have layout kind Auto?

妖精的绣舞 提交于 2019-11-30 17:43:51
The struct System.DateTime and its cousin System.DateTimeOffset have their structure layout kinds set to "Auto". This can be seen with: typeof(DateTime).IsAutoLayout /* true */ or: typeof(DateTime).StructLayoutAttribute.Value /* Auto */ or it can be seen from the IL which declares: .class public auto ansi serializable sealed beforefieldinit System.DateTime ¯¯¯¯ Normally a struct (that is a .NET value type which is not an enum) written with C# will have layout "Sequential" (unless a StructLayoutAttribute has been applied to specify another layout). I searched through some common BCL assemblies,

Why does the System.DateTime struct have layout kind Auto?

隐身守侯 提交于 2019-11-30 01:13:58
问题 The struct System.DateTime and its cousin System.DateTimeOffset have their structure layout kinds set to "Auto". This can be seen with: typeof(DateTime).IsAutoLayout /* true */ or: typeof(DateTime).StructLayoutAttribute.Value /* Auto */ or it can be seen from the IL which declares: .class public auto ansi serializable sealed beforefieldinit System.DateTime ¯¯¯¯ Normally a struct (that is a .NET value type which is not an enum) written with C# will have layout "Sequential" (unless a