rendered paste bodyusing System;
using System.Runtime.InteropServices;
namespace MarshalCopyTester
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Tester
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
DoTest();
}
private static void DoTest()
{
byte[] imagebits = new byte[] {1,2,3,4};
IntPtr ptr;
Console.WriteLine("{0} {1} {2} {3}",imagebits[0],imagebits[1],imagebits[2],imagebits[3]);
ptr = Marshal.AllocHGlobal(4);
Marshal.Copy(imagebits, 0, ptr, imagebits.Length);
Console.WriteLine("{0} {1} {2} {3}",Marshal.ReadByte(ptr),Marshal.ReadByte((IntPtr)(ptr.ToInt32()+Marshal.SizeOf(typeof(byte))) )
,Marshal.ReadByte((IntPtr)(ptr.ToInt32()+2*Marshal.SizeOf(typeof(byte))) ),
Marshal.ReadByte((IntPtr)(ptr.ToInt32()+3*Marshal.SizeOf(typeof(byte))) )
);
// Copy from source to managed
Marshal.Copy(ptr, imagebits, 0, imagebits.Length);
Console.WriteLine("{0} {1} {2} {3}",imagebits[0],imagebits[1],imagebits[2],imagebits[3]);
// Copy from managed to dest
Marshal.Copy(imagebits, 0, ptr, imagebits.Length);
Console.WriteLine("{0} {1} {2} {3}",Marshal.ReadByte(ptr),Marshal.ReadByte((IntPtr)(ptr.ToInt32()+Marshal.SizeOf(typeof(byte))) )
,Marshal.ReadByte((IntPtr)(ptr.ToInt32()+2*Marshal.SizeOf(typeof(byte))) ),
Marshal.ReadByte((IntPtr)(ptr.ToInt32()+3*Marshal.SizeOf(typeof(byte))) )
);
}
}
}