structlayout

Is there an alternative for StructLayout "Pack" attribute in Compact Framework?

I would like to do the following: [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SomeStruct { public byte SomeByte; public int SomeInt; public short SomeShort; public byte SomeByte2; } Is there an alternative since Pack is not supported in the compact framework? Update: Explicitly settin...

C# StructLayout.Explicit Question

I'm trying to understand why the second example below works with no issues, but the first example gives me the exception below. It seems to me that both examples should give an exception based on the description. Can anyone enlighten me? Unhandled Exception: System.TypeLoadException: Could not load type 'StructTest.OuterType' fro...

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

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

What am I doing wrong with this use of StructLayout( LayoutKind.Explicit ) when calling a PInvoke struct with union?

The following is a complete program. It works fine as long as you don't uncomment the '#define BROKEN' at the top. The break is due to a PInvoke failing to marshal a union correctly. The INPUT_RECORD structure in question has a number of substructures that might be used depending on the value in EventType. What I don't understand i...

Shoud I use LayoutKind.Auto for my structs if they don't perform in COM Interop?

By default structs in C# are implemented with [StructLayout( LayoutKind.Sequential )] for reasons basically stating that these type of objects are commonly used for COM Interop and their fields must stay in the order they were defined. Classes have LayoutKind.Auto defined. My question is should I explicitly state my structs as [StructLa...