我试图使用GetCursorPos获取我的光标位置,但它给了我错误
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
此方法的替代方案也将受到赞赏
答案 0 :(得分:2)
您需要在结构中包含LayoutKind
,同时您应该对x和y使用Integer
类型,而不是uint32。最后,方法的返回类型是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。