GetCursorPos - PInvoke error!!

R

Rajesh Soni

Hi!

I'm getting a PInvoke error while trying to execute the following code...


declaration:
Structure POINTAPI

Dim x As IntPtr

Dim y As IntPtr

End Structure

Private Declare Sub GetCursorPos Lib "User32" (ByVal lpPoint As POINTAPI)


usage:
Dim MouseLocation As New POINTAPI

GetCursorPos(MouseLocation)



Please help!



Thanks in advance,

Rajesh
 
T

Tom Shelton

Hi!

I'm getting a PInvoke error while trying to execute the following code...

declaration:
Structure POINTAPI

Dim x As IntPtr

Dim y As IntPtr

End Structure

Private Declare Sub GetCursorPos Lib "User32" (ByVal lpPoint As POINTAPI)

usage:
Dim MouseLocation As New POINTAPI

GetCursorPos(MouseLocation)

Please help!

Thanks in advance,

Rajesh

Structure PointAPI
Public x As Integer ' Int32 if you prefer
Publci y As Integer ' Int32 if you prefer
End Structure

Public Declare Function GetCursorPos Lib user(ByRef lpPoint As
PointAPI) As Boolean
 
T

Tom Shelton

Structure PointAPI
Public x As Integer ' Int32 if you prefer
Publci y As Integer ' Int32 if you prefer
End Structure

Public Declare Function GetCursorPos Lib user(ByRef lpPoint As
PointAPI) As Boolean

woops - hit send to soon...
Public Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As
PointAPI) As Boolean
 
R

Rajesh Soni

Tom,

Thanks Tom, but unfortunately i'm getting the same error :(

A call to PInvoke function 'Form_Name_.Form1::GetCursorPos' has unbalanced
the stack. This is likely because the managed PInvoke signature does not
match the unmanaged target signature. Check that the calling convention and
parameters of the PInvoke signature match the target unmanaged signature.

- Rajesh
 
H

Herfried K. Wagner [MVP]

Rajesh Soni said:
Private Declare Sub GetCursorPos Lib "User32" (ByVal lpPoint As POINTAPI)

In addition to the other replies: Why not use 'Cursor.Position'?
 
T

Tom Shelton

Tom,

Thanks Tom, but unfortunately i'm getting the same error :(

A call to PInvoke function 'Form_Name_.Form1::GetCursorPos' has unbalanced
the stack. This is likely because the managed PInvoke signature does not
match the unmanaged target signature. Check that the calling convention and
parameters of the PInvoke signature match the target unmanaged signature.

- Rajesh






- Show quoted text -

post your code. You have not declared the function correctly.
 
T

Tom Shelton

Tom,

Thanks Tom, but unfortunately i'm getting the same error :(

working code:

Option Explicit On
Option Strict On
Imports System.Runtime.InteropServices


Public Class Form1
Private Structure PointAPI
Public x As Integer
Public y As Integer
End Structure

Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint
As PointAPI) As Boolean

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim pt As PointAPI
If GetCursorPos(pt) Then
MessageBox.Show(String.Format("x={0}, y={1}", pt.x, pt.y))
End If
End Sub
End Class
 
R

Rajesh Soni

Tom,

Thanks a ton!!!!

-Rajesh

Tom Shelton said:
working code:

Option Explicit On
Option Strict On
Imports System.Runtime.InteropServices


Public Class Form1
Private Structure PointAPI
Public x As Integer
Public y As Integer
End Structure

Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint
As PointAPI) As Boolean

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim pt As PointAPI
If GetCursorPos(pt) Then
MessageBox.Show(String.Format("x={0}, y={1}", pt.x, pt.y))
End If
End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top