Skip to content
Merged
Prev Previous commit
Next Next commit
Update pts.cs
  • Loading branch information
live1206 authored Jul 31, 2020
commit 80ac8b939afebabb4c1fb631e3121c24d2ec78a3
119 changes: 81 additions & 38 deletions samples/snippets/csharp/VS_Snippets_CLR/PtrToStructure/CS/pts.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,90 @@
using System;
using System;

using System.Runtime.InteropServices;
namespace Testing
{
class Class1
namespace Testing

{

class Class1

{
// <snippet1>
[StructLayout(LayoutKind.Sequential)]
public class INNER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
// <snippet1>

[StructLayout(LayoutKind.Sequential)]

public class INNER

{

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]

public string field1 = "Test";
}
[StructLayout(LayoutKind.Sequential)]
public struct OUTER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string field1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public byte[] inner; }
[DllImport(@"SomeTestDLL.dll")]
}

[StructLayout(LayoutKind.Sequential)]

public struct OUTER

{

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]

public string field1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]

public byte[] inner;
}
[DllImport(@"SomeTestDLL.dll")]

public static extern void CallTest( ref OUTER po);
static void Main(string[] args)
{
OUTER ed = new OUTER();
INNER[] inn=new INNER[10];
INNER test = new INNER();
static void Main(string[] args)

{

OUTER ed = new OUTER();

INNER[] inn=new INNER[10];

INNER test = new INNER();

int iStructSize = Marshal.SizeOf(test);
int sz =inn.Length * iStructSize;
int sz =inn.Length * iStructSize;

ed.inner = new byte[sz];
try
{
CallTest( ref ed); }
catch(Exception e)
{
Console.WriteLine(e.Message); }
IntPtr buffer = Marshal.AllocCoTaskMem(iStructSize*10);
try

{

CallTest( ref ed);
}

catch(Exception e)

{

Console.WriteLine(e.Message);
}

IntPtr buffer = Marshal.AllocCoTaskMem(iStructSize*10);

Marshal.Copy(ed.inner,0,buffer,iStructSize*10);
int iCurOffset = 0;
for(int i=0;i<10;i++)
int iCurOffset = 0;

for(int i=0;i<10;i++)

{
inn[i] = (INNER)Marshal.PtrToStructure(new
IntPtr(buffer.ToInt32()+iCurOffset),typeof(INNER) );
iCurOffset += iStructSize; }
Console.WriteLine(ed.field1);
Marshal.FreeCoTaskMem(buffer); }
// </snippet1> }}
IntPtr(buffer.ToInt32()+iCurOffset),typeof(INNER) );

iCurOffset += iStructSize;
}

Console.WriteLine(ed.field1);

Marshal.FreeCoTaskMem(buffer);
}
// </snippet1>
}
}