Click here to Skip to main content
15,885,952 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
VB
<DllImport("user32.dll")>
    Public Function WindowFromPoint(ByVal point As Point) As IntPtr
    End Function

I am hoping somebody has a method of getting the windowfrompoint api to work in both 32 bit and 64 bit systems. If that is not possible, is there another api or method to get the window under the mouse cursor?

This is from the pinvoke.net site:
C#
- IntPtr WindowFromPoint(POINT Point) always return 0 in both 32/64 bit process.
- IntPtr WindowFromPoint(int x, int y) works in 32-bit but fails in 64-bit process.

The import statement at the top of this question works for me on a 64 bit system, but not on 32 bit
Posted
Comments
BillWoodruff 13-Oct-14 5:31am    
In .NET 4.0 and later:

bool is64BitOS = Environment.Is64BitOperatingSystem;

http://msdn.microsoft.com/en-us/library/system.environment.is64bitoperatingsystem.aspx

See Raymond Chen's article for the sordid history of MS' API calls to get OS bit-depth:

http://blogs.msdn.com/b/oldnewthing/archive/2010/12/30/10110077.aspx
Craig Haywood 13-Oct-14 6:30am    
Thanks BillWoodruff.

I always used
If IntPtr.Size = 8 Then

 
Share this answer
 
Comments
Craig Haywood 13-Oct-14 4:28am    
Hi Mehdi,

This does not provide a solution. The first link takes me to an incomprehensible solution as I don't even know what language that is. The second link is just the msdn explanation of what the function does and the third uses the exact same declare statement I am using
I've never used WindowFromPoint,but seeing that one signature works in x32 and the other in x64,what i would do is check if my program is running in a 32 or 64 bits OS and call one or the other. To check what type of OS it's running you may check this(StackOverflow)[^]
 
Share this answer
 
Comments
Craig Haywood 13-Oct-14 4:30am    
Where have you found that different signatures works on different bit systems? Can you give an example
Pikoh 13-Oct-14 4:39am    
You have stated that:

- IntPtr WindowFromPoint(int x, int y) works in 32-bit but fails in 64-bit process.

So try using WindowFromPoint(int x, int y) in 32 bits OS and WindowFromPoint(POINT Point) in 64 bits OS
Craig Haywood 13-Oct-14 4:53am    
Indeed I did, but how do you declare the same function twice in the same application?
Pikoh 13-Oct-14 4:56am    
They have different signature,so you can just add this

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

to your app,check what bit OS is it running, and call the method acordingly
Craig Haywood 13-Oct-14 5:10am    
I never knew you could dllimport a function with the same name twice if it has different signitures. Thanks Pikoh, I am going to accept your solution as it seems to be working in initial tests

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900