/
marshal8.cs
125 lines (101 loc) · 2.56 KB
/
marshal8.cs
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Size=1024)]
public class Dummy {
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] a;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
public float[] b;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
public long[] c;
}
[StructLayout(LayoutKind.Sequential)]
class FormattedClass
{
public int i;
public FormattedClass(int i)
{
this.i = i;
}
}
[StructLayout(LayoutKind.Sequential)]
struct Struct
{
public int i;
public Struct(int i)
{
this.i = i;
}
}
public class X {
public static unsafe int Main () {
///
/// Structure to pointer
///
Dummy dummy = new Dummy ();
dummy.a = new byte[16];
dummy.b = new float[16];
dummy.c = new long[16];
for(int i=0; i<16; i++)
dummy.a[i] = (byte)(dummy.b[i] = dummy.c[i] = i+1);
IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Dummy)));
Marshal.StructureToPtr(dummy, p, false);
int offset = (int)Marshal.OffsetOf(typeof(Dummy), "a");
byte *data1 = (byte*)p.ToPointer() + offset;
for(int i=0; i<16; i++) {
if(data1[i] != i+1)
return 1;
}
offset = (int)Marshal.OffsetOf(typeof(Dummy), "b");
float *data2 = (float*)((byte*)p.ToPointer() + offset);
for(int i=0; i<16; i++)
if(data2[i] != i+1)
return 2;
offset = (int)Marshal.OffsetOf(typeof(Dummy), "c");
long *data3 = (long*)((byte*)p.ToPointer() + offset);
for(int i=0; i<16; i++)
if(data3[i] != i+1)
return 3;
///
/// Pointer to structure
///
Dummy dummy2 = new Dummy ();
Marshal.PtrToStructure(p, dummy2);
if(dummy2.a.Length != dummy.a.Length) return 4;
if(dummy2.b.Length != dummy.b.Length) return 5;
if(dummy2.c.Length != dummy.c.Length) return 6;
for(int i=0; i<16; i++)
{
if(dummy2.a[i] != i+1) return 7;
if(dummy2.b[i] != i+1) return 8;
if(dummy2.c[i] != i+1) return 9;
}
Marshal.FreeHGlobal(p);
///
/// Only allow
///
FormattedClass fc = new FormattedClass(20);
IntPtr fc_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FormattedClass)));
Marshal.StructureToPtr(fc, fc_ptr, false);
Marshal.PtrToStructure(fc_ptr, fc);
if (fc.i != 20)
return 10;
Marshal.FreeHGlobal(fc_ptr);
bool exception = false;
try
{
object str = new Struct(20);
IntPtr str_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Struct)));
Marshal.StructureToPtr(str, str_ptr, false);
Marshal.PtrToStructure(str_ptr, str);
Marshal.FreeHGlobal(str_ptr);
}
catch (Exception ex)
{
exception = true;
}
if (!exception)
return 11;
return 0;
}
}