Questions tagged [layoutkind.explicit]
9 questions
13
votes
1 answer
LayoutKind.Sequential not followed when substruct has LayoutKind.Explicit
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()
{
…

Roland Pihlakas
- 4,246
- 2
- 43
- 64
5
votes
3 answers
Boolean Marshalling with LayoutKind.Explicit, Is this broken or failing as designed?
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…

csharptest.net
- 62,602
- 11
- 71
- 89
3
votes
3 answers
Marshalling LayoutKind.Explicit struct with overlapping fails in Release build
I have a struct which has a non-overlapping field reported as overlapped.
[FieldOffset(8)]
Int32 X;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
[FieldOffset(12)]
string Y;
[FieldOffset(28)]
int Z;
The reported error is:
Could not load…

kagali-san
- 2,964
- 7
- 48
- 87
2
votes
1 answer
Is it legitimate to use LayoutKind.Explicit to get around using "unsafe" pointers?
I have code which does XOR on blocks of data using pointers, which is fast, but I'd like to get rid of the "unsafe" requirement on the assembly. If I change it to use LayoutKind.Explicit and overlay a "ulong[]" on top of a "byte[]", I'm basically…

Bryce Wagner
- 2,640
- 1
- 26
- 43
2
votes
2 answers
.NET behaviour of LayoutKind.explicit for a field which is itself a struct
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…

Suzanne Soy
- 3,027
- 6
- 38
- 56
2
votes
2 answers
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…

Taylor Leese
- 51,004
- 28
- 112
- 141
1
vote
2 answers
Union in C# with string incorrectly aligned
I am having "Incorrectly aligned or overlapped by non-object field" error with the following code.
public struct TypeA
{
public string A1;
public string A2;
}
public struct TypeB
{
public string B1,
public string B2;
}
I implemented…

sMah
- 455
- 2
- 8
- 17
1
vote
1 answer
why does mono on atmel processors do not work well with LayoutKind.Explicit?
i have created a structure like bellow :
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public class NodRecord
{
[FieldOffset(0)]
public ushort Driver;
[FieldOffset(2)]
public ushort BaudRate;
…

Mohsen Zahraee
- 3,309
- 5
- 31
- 45
0
votes
2 answers
Exclude extra private field in struct with LayoutKind.Explicit from being part of the structure layout
Let's say we have one structure :
[StructLayout(LayoutKind.Explicit, Size=8)] // using System.Runtime.InteropServices;
public struct AirportHeader {
[FieldOffset(0)]
[MarshalAs(UnmanagedType.I4)]
public int Ident; // a 4 bytes ASCII :…

Karl Stephen
- 1,120
- 8
- 22