Search in sources :

Example 1 with HOOKPROC

use of com.sun.jna.platform.win32.WinUser.HOOKPROC in project jna by java-native-access.

the class User32WindowMessagesTest method hookwinProc.

private HHOOK hookwinProc(HWND hwndToHook) {
    HOOKPROC hookProc = new HOOKPROC() {

        /**
			 * Callback method. cf : https://msdn.microsoft.com/en-us/library/windows/desktop/ms644975(v=vs.85).aspx
			 *
			 * nCode [in] : Specifies whether the hook procedure must process the message. If nCode is HC_ACTION, the
			 * hook procedure must process the message. If nCode is less than zero, the hook procedure must pass the
			 * message to the CallNextHookEx function without further processing and must return the value returned by
			 * CallNextHookEx. wParam [in] : Specifies whether the message was sent by the current thread. If the
			 * message was sent by the current thread, it is nonzero; otherwise, it is zero. lParam [in] : A pointer to
			 * a CWPSTRUCT structure that contains details about the message.
			 *
			 */
        // used by introspection from jna.
        @SuppressWarnings("unused")
        public LRESULT callback(int nCode, WPARAM wParam, LPARAM lParam) {
            if (nCode < 0) {
                return User32.INSTANCE.CallNextHookEx(null, nCode, wParam, lParam);
            }
            try {
                WinUser.CWPSTRUCT cwp = new WinUser.CWPSTRUCT(new Pointer(lParam.longValue()));
                switch(cwp.message) {
                    case WinUser.WM_USER:
                        {
                            HWND hWndSource = new HWND(new Pointer(cwp.wParam.longValue()));
                            log(cwp.hwnd + " - Received a message from " + hWndSource + " hooked proc : code= " + cwp.wParam + ", value = " + cwp.lParam);
                            assertEqualsForCallbackExecution(MSG_HOOKED_CODE, cwp.wParam.intValue());
                            assertEqualsForCallbackExecution(MSG_HOOKED_VAL, cwp.lParam.intValue());
                            return new LRESULT(0);
                        }
                }
                // Send message to next hook.
                return User32.INSTANCE.CallNextHookEx(null, nCode, wParam, lParam);
            } catch (Throwable t) {
                t.printStackTrace();
                return new LRESULT(0);
            }
        }
    };
    HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null);
    int threadtoHook = User32.INSTANCE.GetWindowThreadProcessId(hwndToHook, null);
    // Hook of the wndProc
    return User32.INSTANCE.SetWindowsHookEx(WinUser.WH_CALLWNDPROC, hookProc, hInst, threadtoHook);
}
Also used : LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) HINSTANCE(com.sun.jna.platform.win32.WinDef.HINSTANCE) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) HWND(com.sun.jna.platform.win32.WinDef.HWND) Pointer(com.sun.jna.Pointer) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) HOOKPROC(com.sun.jna.platform.win32.WinUser.HOOKPROC)

Aggregations

Pointer (com.sun.jna.Pointer)1 HINSTANCE (com.sun.jna.platform.win32.WinDef.HINSTANCE)1 HWND (com.sun.jna.platform.win32.WinDef.HWND)1 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)1 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)1 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)1 HOOKPROC (com.sun.jna.platform.win32.WinUser.HOOKPROC)1