Search code examples
c#.netwinforms64-bit

WindowFromPoint doesn't work correctly on 64bit .NET Framework


I wrote the easy test program by C# and .NET 4.0.

[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

Point pt = MousePosition;
IntPtr w = WindowFromPoint(pt.X, pt.Y);

If this builts as 32bit, it works. But if it builds as 64bit, an unrelated window handle will return. Are there a solution or alternatives?


Solution

  • Right, that won't work. WindowFromPoint() does not take two arguments, it only takes one. A structure of type POINT. You got away with it in 32-bit code by sheer accident, that luck ran out in 64-bit mode since it passes arguments a different way.

    Use the pinvoke.net web site to find the correct pinvoke declaration.