2017-10-20 103 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