Questions tagged [structlayout]

49 questions
1
vote
1 answer

Difference between [StructLayout(LayoutKind.Sequential)] and TypeAttributes.SequentialLayout?

I dynamically build a struct for P/Invoke using const TypeAttributes typeAttributes = TypeAttributes.Public | TypeAttributes.SequentialLayout | …
Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
1
vote
0 answers

FxCop warning won't go away

An object of this class will be passed to an API call. I implemented the first suggestion from the warning below: To reduce security risk, marshal field 'Resource.k__BackingField' as Unicode, by setting StructLayout.CharSet on 'Resource' to…
ajeh
  • 2,652
  • 2
  • 34
  • 65
1
vote
1 answer

Problems with Marshal.PtrToStructure and String

I've the following class [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public class xy11Dataset : SZLDataset { public short Index { get; set; } [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)] private…
Jochen Kühner
  • 1,385
  • 2
  • 18
  • 43
1
vote
1 answer

Does "DefaultCharSetAttribute" apply to "StructLayoutAttribute.CharSet" too?

C# default platform invoke uses ANSI marshalling both for method parameters and struct fields strings. System.Runtime.InteropServices contains an attribute "DefaultCharset" to change it to Unicode. From MSDN: "Apply the DefaultCharSetAttribute…
1
vote
1 answer

FatalExecutionEngineError when messing with StructLayouts

Im messing arround with structlayouts and I found some thing i find quite odd: The following code is working as i thought it would: using System; using System.Runtime.InteropServices; public class Program { [STAThread] static void Main() …
CSharpie
  • 9,195
  • 4
  • 44
  • 71
1
vote
6 answers

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…
csharptest.net
  • 62,602
  • 11
  • 71
  • 89
1
vote
4 answers

StructLayout to convert byte array to short

Sorry about the horrible title but I honestly have know idea what I want nor what is wrong... Basically I have a struct (well I have 250+ structs but they all follow the same idea) that looks like this: [StructLayout(LayoutKind.Explicit)] public…
jduncanator
  • 2,154
  • 1
  • 22
  • 39
0
votes
2 answers

Marshaling an unmanaged struct containing a char* member

I am working with a callback function going from unmanged code to my managed C# code. The callback has a parameter void* eventData. EventData could be several different struct types. In my C# code I define eventData as an IntPtr and use …
Dan Vogel
  • 3,858
  • 7
  • 41
  • 57
0
votes
1 answer

Pad a struct so its size is a multiple of 16 bytes

I'm trying to have my C# struct match up some complex padding and packing rules. Fields should be aligned on 4 byte boundaries. The entire struct should be a multiple of 16 bytes Using the StructLayout attribute I can make sure that fields are…
Roy T.
  • 9,429
  • 2
  • 48
  • 70
0
votes
0 answers

C to C# struct with union byte array Map

typedef union { volatile struct { //Init UINT64 ARG1 : 1; //800H UINT64 ARG2 : 1; //800H UINT64 ARG3 : 1; //00H …
0
votes
1 answer

nesting arrays of C# classes

I posted a related question a few weeks ago : Marshal.Sizeof() returning unexpected value As background, we hired a third party to convert an old C++ project to C#. This is a communications protocol application that sends/receives messages over…
buzzard51
  • 1,372
  • 2
  • 23
  • 40
0
votes
1 answer

C# StructLayout and dispose

I have designed a StructLayout named ReceiveBuffer, and inside ReceiveBuffer, I have an unmanaged type of array to store data. The code is shown as below. [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)] public struct…
CivilKen
  • 15
  • 5
0
votes
3 answers

Fixed sbyte[] to string C#

I'm trying to get a string from memory using StrucLayout and FieldOffset But I'm having a lot of trouble understanding how byte works. Here is my code ATM : [StructLayout(LayoutKind.Explicit)] public unsafe struct InfoDetails { …
Cesar
  • 453
  • 9
  • 21
0
votes
1 answer

undefined behaviour with fieldoffset

i have extrated with success a structure object from a valid pointer using the PtrToStructure function (in vb.NET), the problem is the field "FrameRects" has invalid values comparing with the values obtained from a C++ sample: the vb.NET code is…
0
votes
2 answers

C# StructLayout Pack=?? for use with bool values

In C#, I am creating multiple different structs that contain 16 variables of type bool. I will have several different of these structs that will then be combined with other data types into more complex structs. I am needing to have them treated as…
bakerjw
  • 1
  • 2