10 out of 19 rated this helpful - Rate this topic

GetCursorPos function

Applies to: desktop apps only

Retrieves the cursor's position, in screen coordinates.

Syntax

BOOL WINAPI GetCursorPos(
  __out  LPPOINT lpPoint
);

Parameters

lpPoint [out]

Type: LPPOINT

A pointer to a POINT structure that receives the screen coordinates of the cursor.

Return value

Type: BOOL

Returns nonzero if successful or zero otherwise. To get extended error information, call GetLastError.

Remarks

The cursor position is always specified in screen coordinates and is not affected by the mapping mode of the window that contains the cursor.

The calling process must have WINSTA_READATTRIBUTES access to the window station.

The input desktop must be the current desktop when you call GetCursorPos. Call OpenInputDesktop to determine whether the current desktop is the input desktop. If it is not, call SetThreadDesktop with the HDESK returned by OpenInputDesktop to switch to that desktop.

Examples

For an example, see Using the Keyboard to Move the Cursor.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
ClipCursor
GetCursorInfo
SetCursor
SetCursorPos
ShowCursor
GetMessagePos
Conceptual
Cursors
Other Resources
POINT

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
GetCursorPos Example Code
$0POINT cursorPos;
GetCursorPos(&cursorPos);

int x = (int) cursorPos.x;
int y = (int) cursorPos.y;

// work with x & y
$0
$0$0
GetCursorPos / Windows 7
The LAA bug in GetCursorPos under WOW64 appears to be fixed in Windows 7.

However, it hasn't been down-ported to Vista and I doubt that it will so the solution of using GetCursorInfo seems to be the best strategy for dealing with it.
GetCursorPos calls may fail in LAA applications
That implies that GetCursorPos should not be used by Large Address Aware applications.

GetCursorInfo seems to work fine, so consider using it instead.


To reproduce the problem:

On Vista x64:

Set the following registry value to 0x100000:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Memory Management\AllocationPreference
and reboot

Enable the Large Address Aware (LAA) flag for the application, e.g. editbin /LARGEADDRESSAWARE test.exe

Run the application

GetCursorPos will fail but GetCursorInfo seems to work fine.

C# syntax
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point pt);
vb.net syntax
<DllImport("user32.dll")> _
Public Shared Function GetCursorPos(<[In], Out> ByVal pt As Point) As Boolean End Function