using System;
using System.Runtime.InteropServices;
namespace MarshalCopyTester
{
///
/// Summary description for Form1.
///
public class Tester
{
///
/// The main entry point for the application.
///
[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))) )
);
}
}
}