我的目標調用非託管代碼
我想左擊轉換爲右鍵單擊PInvokeStackImbalance - 從HookCallback
我的做法
- 我註冊通過調用SetWindowsHookEx低級別掛鉤(USER32 .dll)
- 篩選鼠標左鍵單擊
- 檢查我是否想翻譯該特定點擊
- 的情況下,我真的想
- 不傳遞消息
- 創建通過mouse_event一個新的鼠標點擊(user32.dll中太)
問題
當我做這樣的描述:
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
if(nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam && doRight) {
doRight = false;
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
mouse_event(/*right down + right up*/8 | 16, hookStruct.pt.x, hookStruct.pt.y, 0, 0);
return new IntPtr(1);
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
對mouse_event的調用失敗,出現PInvokeStackImpalance異常,我想我應該關心這個異常。
DllImports
因爲通常一個PInvokeStackImbalance談到由於不正確的進口簽名這裏是我的:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
出現
我找出問題的正常途徑在這種情況下,失敗 - 因爲本身的mouse_event-call會起作用,左鍵點擊的廢棄也起作用。我討厭的結構比部分的總和更多...
該死的我愚蠢 - 我甚至說,通常簽名是錯誤的...但現在我想知道 - 我複製了另一個項目(生產代碼)的信號,它在那裏工作...發生了什麼? – santa 2010-08-10 11:16:45
P/Invoke在某種程度上可以原諒不正確的簽名,儘管在您的情況下不正確的數據可能會傳遞給函數。要警告您簽名不正確,PInvokeStackImbalance託管調試助手會在激活時觸發。 – 2010-08-10 11:23:56