I've been wondering for a while if the System.Drawing.Rectangle structure could be used for PInvoke in place of the Win32 RECT structure, so I thought I would write a little application to verify my guess (overkill, I know... but fun none the less.) The output: 1,2,3,4 and 1,2,2,2. If they were compatible they would have been the same, therefore you cannot use the Rectangle structure interchangeably with the RECT structure for PInvoke.
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);
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u
Page rendered at Tuesday, October 07, 2008 12:13:59 AM (Central Daylight Time, UTC-05:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.