Skip to content

Commit 80ac8b9

Browse files
authored
Update pts.cs
1 parent b40a505 commit 80ac8b9

File tree

1 file changed

+81
-38
lines changed
  • samples/snippets/csharp/VS_Snippets_CLR/PtrToStructure/CS

1 file changed

+81
-38
lines changed
Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,90 @@
1-
using System;
1+
using System;
2+
23
using System.Runtime.InteropServices;
3-
namespace Testing
4-
{
5-
class Class1
4+
namespace Testing
5+
6+
{
7+
8+
class Class1
9+
610
{
7-
// <snippet1>
8-
[StructLayout(LayoutKind.Sequential)]
9-
public class INNER
10-
{
11-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
11+
// <snippet1>
12+
13+
[StructLayout(LayoutKind.Sequential)]
14+
15+
public class INNER
16+
17+
{
18+
19+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
20+
1221
public string field1 = "Test";
13-
}
14-
[StructLayout(LayoutKind.Sequential)]
15-
public struct OUTER
16-
{
17-
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
18-
public string field1;
19-
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
20-
public byte[] inner; }
21-
[DllImport(@"SomeTestDLL.dll")]
22+
}
23+
24+
[StructLayout(LayoutKind.Sequential)]
25+
26+
public struct OUTER
27+
28+
{
29+
30+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
31+
32+
public string field1;
33+
34+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
35+
36+
public byte[] inner;
37+
}
38+
[DllImport(@"SomeTestDLL.dll")]
39+
2240
public static extern void CallTest( ref OUTER po);
23-
static void Main(string[] args)
24-
{
25-
OUTER ed = new OUTER();
26-
INNER[] inn=new INNER[10];
27-
INNER test = new INNER();
41+
static void Main(string[] args)
42+
43+
{
44+
45+
OUTER ed = new OUTER();
46+
47+
INNER[] inn=new INNER[10];
48+
49+
INNER test = new INNER();
50+
2851
int iStructSize = Marshal.SizeOf(test);
29-
int sz =inn.Length * iStructSize;
52+
int sz =inn.Length * iStructSize;
53+
3054
ed.inner = new byte[sz];
31-
try
32-
{
33-
CallTest( ref ed); }
34-
catch(Exception e)
35-
{
36-
Console.WriteLine(e.Message); }
37-
IntPtr buffer = Marshal.AllocCoTaskMem(iStructSize*10);
55+
try
56+
57+
{
58+
59+
CallTest( ref ed);
60+
}
61+
62+
catch(Exception e)
63+
64+
{
65+
66+
Console.WriteLine(e.Message);
67+
}
68+
69+
IntPtr buffer = Marshal.AllocCoTaskMem(iStructSize*10);
70+
3871
Marshal.Copy(ed.inner,0,buffer,iStructSize*10);
39-
int iCurOffset = 0;
40-
for(int i=0;i<10;i++)
72+
int iCurOffset = 0;
73+
74+
for(int i=0;i<10;i++)
75+
4176
{
4277
inn[i] = (INNER)Marshal.PtrToStructure(new
43-
IntPtr(buffer.ToInt32()+iCurOffset),typeof(INNER) );
44-
iCurOffset += iStructSize; }
45-
Console.WriteLine(ed.field1);
46-
Marshal.FreeCoTaskMem(buffer); }
47-
// </snippet1> }}
78+
IntPtr(buffer.ToInt32()+iCurOffset),typeof(INNER) );
79+
80+
iCurOffset += iStructSize;
81+
}
82+
83+
Console.WriteLine(ed.field1);
84+
85+
Marshal.FreeCoTaskMem(buffer);
86+
}
87+
// </snippet1>
88+
}
89+
}
90+

0 commit comments

Comments
 (0)