Search in sources :

Example 1 with HHOOK

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

the class User32WindowMessagesTest method testWindowMesssages.

/**
	 * Instantiates 2 windows and make them communicate through windows messages, even complex ones throught
	 * WM_COPYDATA.
	 */
@Test
public void testWindowMesssages() {
    // note : check the asserts that are present in the callback implementations here after.
    // Create window 1 named "ping"
    createWindow("ping");
    // let windows create the window before searching its handle.
    sleepCurrThread(4000);
    // Retrieves the created window's handle.
    HWND hwndPing = determineHWNDFromWindowClass("ping");
    assertNotNull(hwndPing);
    HHOOK hook = null;
    try {
        // DEMO 1 : sends a simple message to ping window with code MSG_CODE and value 123456.
        LRESULT result = User32.INSTANCE.SendMessage(hwndPing, WinUser.WM_USER, new WPARAM(MSG_SIMPLE_CODE), new LPARAM(MSG_SIMPLE_VAL));
        log("User Message sent to " + hwndPing + ", result = " + result);
        assertEquals(0, result.doubleValue(), 0);
        // DEMO 2 : send of structured message.
        // copyDataStruct must be held strongly on the java side.
        // cf : https://github.com/java-native-access/jna/pull/774 comments.
        COPYDATASTRUCT copyDataStruct = createStructuredMessage();
        result = User32.INSTANCE.SendMessage(hwndPing, WinUser.WM_COPYDATA, null, /* No current hwnd for this demo */
        new LPARAM(Pointer.nativeValue(copyDataStruct.getPointer())));
        log("COPYDATASTRUCT sent message to " + hwndPing + "(size=" + copyDataStruct.size() + ") code =" + copyDataStruct.dwData);
        assertEquals(0, result.intValue());
        assertEquals(DATA_STRUCT_CODE, copyDataStruct.dwData.doubleValue(), 0);
        // DEMO 3 : hook winproc then send a message to the hooked proc.
        hook = hookwinProc(hwndPing);
        result = User32.INSTANCE.SendMessage(hwndPing, WinUser.WM_USER, new WPARAM(MSG_HOOKED_CODE), new LPARAM(MSG_HOOKED_VAL));
        log("User Message sent to hooked proc " + hwndPing + ", result = " + result);
        assertEquals(0, result.intValue());
        // Waits e few moment before shutdown message.
        sleepCurrThread(3000);
    } finally {
        try {
            // checks that there has been no exception in the created thread for windows messages receival.
            assertNull("Unexpected exception in created Thread : " + exceptionInCreatedThread, exceptionInCreatedThread);
        //assert done in a try block in order not to block the WM_CLOSE message sending.
        } finally {
            User32.INSTANCE.PostMessage(hwndPing, WinUser.WM_CLOSE, null, null);
            log("WM_CLOSE posted to " + hwndPing);
            // Remember to unhook the win proc.
            if (hook != null) {
                User32.INSTANCE.UnhookWindowsHookEx(hook);
            }
            log("Unhook done correctly");
        }
    }
}
Also used : HHOOK(com.sun.jna.platform.win32.WinUser.HHOOK) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) HWND(com.sun.jna.platform.win32.WinDef.HWND) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) COPYDATASTRUCT(com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) Test(org.junit.Test)

Example 2 with HHOOK

use of com.sun.jna.platform.win32.WinUser.HHOOK 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

HWND (com.sun.jna.platform.win32.WinDef.HWND)2 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)2 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)2 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)2 Pointer (com.sun.jna.Pointer)1 HINSTANCE (com.sun.jna.platform.win32.WinDef.HINSTANCE)1 COPYDATASTRUCT (com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)1 HHOOK (com.sun.jna.platform.win32.WinUser.HHOOK)1 HOOKPROC (com.sun.jna.platform.win32.WinUser.HOOKPROC)1 Test (org.junit.Test)1