Entwickler-Ecke

Sonstiges (.NET) - FieldOffset probloblem


KokaLola - Sa 30.10.10 00:58
Titel: FieldOffset probloblem
Moin - ich habe ein problem unzwar möchte ich dieses Stuct union (c++) in c# convertieren nur versteh ich es iwie doch nicht...

C++-Code:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
********************************************
union MyType{
  int   i_Param;
  float   f_Param;
  DWORD  d_Param;
  //wchar_t wc_Param[50];
};

struct S_Header{
  unsigned short header;
  unsigned short type;
  MyType LParam,MParam,RParam,
    returnValue1,
    returnValue2;
};
********************************************


Mein Versuch in c#:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
********************************************
        public struct S_Header
        {
            [FieldOffset(0)]
            public ushort header;
            [FieldOffset(2)]
            public ushort type;
            [FieldOffset(8)]
            public int i_LParam;
            [FieldOffset(8)]
            public float f_LParam;
            [FieldOffset(12)]
            public int i_MParam;
            [FieldOffset(12)]
            public float f_MParam;
            [FieldOffset(16)]
            public int i_RParam;
            [FieldOffset(16)]
            public float f_RParam;
            [FieldOffset(20)]
            public int i_returnValue1;
            [FieldOffset(20)]
            public float f_returnValue1;
            [FieldOffset(24)]
            public int i_returnValue2;
            [FieldOffset(24)]
            public float f_returnValue2;
        }
********************************************



Vielleicht weiß ja jmd. was ich falsch gemacht habe.....


Mit freundlichen Grüßen

Moderiert von user profile iconKha: C#-Tags hinzugefügt


Greenberet - Sa 30.10.10 19:11

Probiers mal mit


C#-Quelltext
1:
2:
3:
4:
[StructLayout(LayoutKind.Explicit)]
public struct S_Header
{
//...


jaenicke - Sa 30.10.10 19:16

Außerdem kannst du nicht zwei gleiche Offsets angeben. Wie sollen zwei Informationen an der physikalisch gleichen Stelle stehen? :shock:


Kha - Sa 30.10.10 19:47

user profile iconjaenicke hat folgendes geschrieben Zum zitierten Posting springen:
Außerdem kannst du nicht zwei gleiche Offsets angeben.
Äh, das ist doch der ganze Witz an LayoutKind.Explicit ;) ...


jaenicke - Sa 30.10.10 19:50

Ja, jetzt seh ichs auch wie der ursprüngliche Quelltext in C++ gemeint ist. :oops: