2017-10-20 69 views
0

我试图获得使用GetCursorPos我的光标的位置,但它给我的错误光标位置给PInvokeStackImbalance错误

Managed Debugging Assistant 'PInvokeStackImbalance' : 'A call to PInvoke function 'MoveClickApp!MoveClickApp.Module1::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.' 

我无法弄清楚这意味着什么或如何进行。有任何想法吗?

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

Public Structure POINTAPI 
    Dim x As UInt32 
    Dim y As UInt32 
End Structure 

Public Function GetX() As UInt32 
    Dim n As POINTAPI 
    GetCursorPos(n) 
    GetX = n.x 
End Function 

Public Function GetY() As UInt32 
    Dim n As POINTAPI 
    GetCursorPos(n) 
    GetY = n.y 
End Function 

替代这种方法也可以,你也应该使用Integer类型x和y,不UINT32赞赏

回答

2

您需要包括在你的结构LayoutKind。最后,该方法的返回类型是BOOL不是UINT32和你应该当元帅的结果:

<System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> 
Public Structure POINTAPI 
    Dim x As Integer 
    Dim y As Integer 
End Structure 

<DllImport("user32.dll", ExactSpelling := True, SetLastError := True)> _ 
Public Shared Function GetCursorPos(ByRef lpPoint As POINTAPI) As <MarshalAs(UnmanagedType.Bool)> Boolean 
End Function 

Pinvoke.net是要调用Win32 API的时候用一个很好的资源。他们的样品有这种方法here

+0

这有帮助!我实际上没有看到这些<>命令不是编组vb之前,所以这是我需要查找完全理解。奇怪的是,这个修复程序使得一个无关的SetCursorPos命令现在发生了故障,但我会尝试自己弄清楚。谢谢您的帮助 – Zyzyx