using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace RECTvsRectangle
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
static class Program
{
static void Main()
{
int intSize = Marshal.SizeOf(typeof(int));
int rectangleSize = Marshal.SizeOf(typeof(Rectangle));
int rectSize = Marshal.SizeOf(typeof(RECT));
RECT r1;
r1.Left = 1;
r1.Top = 2;
r1.Right = 3; //width = 2
r1.Bottom = 4; //height = 2
Rectangle r2 = Rectangle.Empty;
r2.X = 1;
r2.Y = 2;
r2.Width = 2;
r2.Height = 2;
IntPtr p1 = Marshal.AllocCoTaskMem(rectSize);
Marshal.StructureToPtr(r1, p1, true);
IntPtr p2 = Marshal.AllocCoTaskMem(rectangleSize);
Marshal.StructureToPtr(r2, p2, true);
for (int i = 0; i < rectSize; i += intSize)
{
Console.WriteLine("Value: " + Marshal.ReadInt32(p1, i).ToString());
}
Console.WriteLine("---");
for (int i = 0; i < rectangleSize; i += intSize)
{
Console.WriteLine("Value: " + Marshal.ReadInt32(p2, i).ToString());
}
Marshal.FreeCoTaskMem(p1);
Marshal.FreeCoTaskMem(p2);
}
}
}