Search code examples
winapivb6cursor-position

GetCursorPos only returning x value


I saw a thread on an MSDN forum where there was an issue with 32-bit vs. 64-bit integers. I'm not sure if that is my issue, but it seems as though this code should work, so I'm a bit confused.

I'm running VB6 in compatiblity mode (XP SP2) in Windows 7 64-bit.

Type POINTAPI ' This holds the logical cursor information
    x As Integer
    y As Integer
End Type

Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

In Timer1_Timer()...

Dim mousePos As POINTAPI
Call GetCursorPos(mousePos)
MsgBox mousePos.x & " " & mousePos.y

This message box shows the correct value for the x coordinate of the mouse, but it shows "0" for y, no matter where the mouse is on the screen. Also, GetCursorPos() is returning 1.


Solution

  • In VB6 the Integer data type is a 16-bit number. You have to use Long as this is a 32-bit number.

    Type POINTAPI ' This holds the logical cursor information
      x As Long
      y As Long
    End Type
    
    Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    

    or use:

    Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINT_TYPE) As Long