2 // Tests for Marshal.StructureToPtr and PtrToStructure
7 using System
.Runtime
.InteropServices
;
12 [StructLayout (LayoutKind
.Sequential
)]
13 public class SimpleObj
{
17 public void test () {}
20 [StructLayout (LayoutKind
.Sequential
)]
21 public struct SimpleStruct2
{
26 [StructLayout (LayoutKind
.Sequential
, CharSet
=CharSet
.Ansi
)]
27 public struct SimpleStruct
{
32 [MarshalAs (UnmanagedType
.ByValArray
, SizeConst
=2)] public short[] a1
;
33 [MarshalAs (UnmanagedType
.ByValTStr
, SizeConst
=4)] public string s1
;
34 public SimpleStruct2 emb1
;
35 public SimpleObj emb2
;
38 [MarshalAs (UnmanagedType
.ByValArray
, SizeConst
=2)] public char[] a2
;
41 [StructLayout (LayoutKind
.Sequential
, CharSet
=CharSet
.Ansi
)]
42 public struct ByValTStrStruct
{
43 [MarshalAs (UnmanagedType
.ByValTStr
, SizeConst
=4)] public string s1
;
47 [StructLayout (LayoutKind
.Sequential
, CharSet
=CharSet
.Unicode
)]
48 public struct ByValWStrStruct
{
49 [MarshalAs (UnmanagedType
.ByValTStr
, SizeConst
=4)] public string s1
;
53 [StructLayout (LayoutKind
.Sequential
, Pack
=1)]
54 public struct PackStruct1
{
58 [StructLayout (LayoutKind
.Sequential
)]
59 public struct PackStruct2
{
64 public unsafe static int Main (String
[] args
) {
65 if (TestDriver
.RunTests (typeof (Tests
), args
) != 0)
70 public static int test_0_structure_to_ptr () {
71 SimpleStruct ss
= new SimpleStruct ();
72 int size
= Marshal
.SizeOf (typeof (SimpleStruct
));
77 IntPtr p
= Marshal
.AllocHGlobal (size
);
82 ss
.a1
= new short [2];
86 ss
.emb1
= new SimpleStruct2 ();
89 ss
.emb2
= new SimpleObj ();
92 ss
.s2
= "just a test";
98 Marshal
.StructureToPtr (ss
, p
, false);
99 Type t
= ss
.GetType ();
101 if (Marshal
.ReadInt32 (p
, (int)Marshal
.OffsetOf (t
, "a")) != 1)
103 if (Marshal
.ReadInt32 (p
, (int)Marshal
.OffsetOf (t
, "bool1")) != 1)
105 if (Marshal
.ReadInt32 (p
, (int)Marshal
.OffsetOf (t
, "bool2")) != 0)
107 if (Marshal
.ReadInt32 (p
, (int)Marshal
.OffsetOf (t
, "b")) != 2)
109 if (Marshal
.ReadInt16 (p
, 16) != 6)
111 if (Marshal
.ReadInt16 (p
, 18) != 5)
113 if (Marshal
.ReadByte (p
, 20) != 97)
115 if (Marshal
.ReadByte (p
, 21) != 98)
117 if (Marshal
.ReadByte (p
, 22) != 99)
119 if (Marshal
.ReadByte (p
, 23) != 0)
121 if (Marshal
.ReadInt32 (p
, 24) != 3)
123 if (Marshal
.ReadInt32 (p
, 28) != 4)
125 if (Marshal
.ReadInt32 (p
, 32) != 10)
127 if (Marshal
.ReadInt32 (p
, 36) != 11)
129 if (Marshal
.ReadByte (p
, (int)Marshal
.OffsetOf (t
, "a2")) != 97)
131 if (Marshal
.ReadByte (p
, (int)Marshal
.OffsetOf (t
, "a2") + 1) != 98)
134 SimpleStruct cp
= (SimpleStruct
)Marshal
.PtrToStructure (p
, ss
.GetType ());
139 if (cp
.bool1
!= true)
142 if (cp
.bool2
!= false)
169 if (cp
.s2
!= "just a test")
175 if (cp
.a2
[0] != 'a')
178 if (cp
.a2
[1] != 'b')
183 [StructLayout(LayoutKind
.Sequential
, Pack
= 1, CharSet
= CharSet
.Unicode
)]
184 public struct Struct1
186 [MarshalAs(UnmanagedType
.ByValTStr
, SizeConst
= 8)]
187 public string Field1
;
188 [MarshalAs(UnmanagedType
.ByValTStr
, SizeConst
= 10)]
189 public string Field2
;
190 [MarshalAs(UnmanagedType
.ByValTStr
, SizeConst
= 14)]
191 public string Field3
;
194 public static int test_0_byvaltstr () {
195 ByValTStrStruct s
= new ByValTStrStruct ();
197 IntPtr p2
= Marshal
.AllocHGlobal (Marshal
.SizeOf (typeof (ByValTStrStruct
)));
198 Marshal
.StructureToPtr(s
, p2
, false);
200 /* Check that the ByValTStr is initialized correctly */
201 for (int i
= 0; i
< 4; ++i
)
202 if (Marshal
.ReadByte (p2
, i
) != 0)
208 Marshal
.StructureToPtr(s
, p2
, false);
210 ByValTStrStruct s2
= (ByValTStrStruct
)Marshal
.PtrToStructure (p2
, typeof (ByValTStrStruct
));
212 /* The fourth char is lost because of null-termination */
219 // Check that decoding also respects the size, even when there is no null terminator
220 byte[] data
= Encoding
.ASCII
.GetBytes ("ABCDXXXX");
221 int size
= Marshal
.SizeOf (typeof (ByValTStrStruct
));
222 IntPtr buffer
= Marshal
.AllocHGlobal (size
);
223 Marshal
.Copy (data
, 0, buffer
, size
);
225 s2
= (ByValTStrStruct
)Marshal
.PtrToStructure (buffer
, typeof (ByValTStrStruct
));
232 public static int test_0_byvaltstr_unicode () {
233 ByValWStrStruct s
= new ByValWStrStruct ();
235 IntPtr p2
= Marshal
.AllocHGlobal (Marshal
.SizeOf (typeof (ByValWStrStruct
)));
236 Marshal
.StructureToPtr(s
, p2
, false);
238 /* Check that the ByValWStr is initialized correctly */
239 for (int i
= 0; i
< 8; ++i
)
240 if (Marshal
.ReadByte (p2
, i
) != 0)
246 Marshal
.StructureToPtr(s
, p2
, false);
248 ByValWStrStruct s2
= (ByValWStrStruct
)Marshal
.PtrToStructure (p2
, typeof (ByValWStrStruct
));
250 /* The fourth char is lost because of null-termination */
259 public static int test_0_byvaltstr_max_size () {
260 string buffer
= "12345678123456789012345678901234";
262 IntPtr ptr
= Marshal
.StringToBSTR (buffer
);
264 Struct1 data
= (Struct1
)Marshal
.PtrToStructure (ptr
, typeof (Struct1
));
265 if (data
.Field1
!= "12345678")
267 if (data
.Field2
!= "1234567890")
269 if (data
.Field3
!= "12345678901234")
274 // Check that the 'Pack' directive on a struct changes the min alignment of the struct as well (#12110)
275 public static int test_0_struct_pack () {
276 if (Marshal
.OffsetOf (typeof (PackStruct2
), "s") != new IntPtr (1))