Search in sources :

Example 1 with WPARAM

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

use of com.sun.jna.platform.win32.WinDef.WPARAM in project jna by java-native-access.

the class User32Test method testSendMessage.

@Test
public void testSendMessage() {
    DesktopWindow explorerProc = getWindowByProcessPath("explorer.exe");
    assertNotNull(explorerProc);
    LRESULT result = User32.INSTANCE.SendMessage(explorerProc.getHWND(), WinUser.WM_USER, new WPARAM(124), new LPARAM(12345));
    assertNotEquals(0, result);
}
Also used : LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) DesktopWindow(com.sun.jna.platform.DesktopWindow) Test(org.junit.Test)

Example 3 with WPARAM

use of com.sun.jna.platform.win32.WinDef.WPARAM in project jna by java-native-access.

the class User32Test method testSendMessageTimeout.

@Test
public void testSendMessageTimeout() {
    DesktopWindow explorerProc = getWindowByProcessPath("explorer.exe");
    assertNotNull(explorerProc);
    final DWORDByReference hIconNumber = new DWORDByReference();
    LRESULT result = User32.INSTANCE.SendMessageTimeout(explorerProc.getHWND(), WinUser.WM_GETICON, new WPARAM(WinUser.ICON_BIG), new LPARAM(0), WinUser.SMTO_ABORTIFHUNG, 500, hIconNumber);
    assertNotEquals(0, result);
}
Also used : LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) DesktopWindow(com.sun.jna.platform.DesktopWindow) Test(org.junit.Test)

Example 4 with WPARAM

use of com.sun.jna.platform.win32.WinDef.WPARAM in project jna by java-native-access.

the class WindowUtilsTest method testGetIconSize.

public void testGetIconSize() throws Exception {
    final JFrame w = new JFrame();
    try {
        final BufferedImage expectedIcon = ImageIO.read(new FileInputStream(new File(getClass().getResource("/res/test_icon.png").getPath())));
        w.setIconImage(expectedIcon);
        w.setVisible(true);
        Pointer p = Native.getComponentPointer(w);
        assertNotNull("Could not obtain native HANDLE for JFrame", p);
        HWND hwnd = new HWND(p);
        final DWORDByReference hIconNumber = new DWORDByReference();
        LRESULT result = User32.INSTANCE.SendMessageTimeout(hwnd, WinUser.WM_GETICON, new WPARAM(WinUser.ICON_BIG), new LPARAM(0), WinUser.SMTO_ABORTIFHUNG, 500, hIconNumber);
        assertNotEquals(0, result.intValue());
        final HICON hIcon = new HICON(new Pointer(hIconNumber.getValue().longValue()));
        assertTrue(WindowUtils.getIconSize(hIcon).width >= 32);
        assertTrue(WindowUtils.getIconSize(hIcon).height >= 32);
        assertEquals(WindowUtils.getIconSize(hIcon).width, WindowUtils.getIconSize(hIcon).height);
    } finally {
        w.dispose();
    }
}
Also used : LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) JFrame(javax.swing.JFrame) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) HWND(com.sun.jna.platform.win32.WinDef.HWND) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) HICON(com.sun.jna.platform.win32.WinDef.HICON) Pointer(com.sun.jna.Pointer) File(java.io.File) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) BufferedImage(java.awt.image.BufferedImage) FileInputStream(java.io.FileInputStream)

Example 5 with WPARAM

use of com.sun.jna.platform.win32.WinDef.WPARAM in project jna by java-native-access.

the class KeyHook method main.

public static void main(String[] args) {
    final User32 lib = User32.INSTANCE;
    HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    keyboardHook = new LowLevelKeyboardProc() {

        @Override
        public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
            if (nCode >= 0) {
                switch(wParam.intValue()) {
                    case WinUser.WM_KEYUP:
                    case WinUser.WM_KEYDOWN:
                    case WinUser.WM_SYSKEYUP:
                    case WinUser.WM_SYSKEYDOWN:
                        System.err.println("in callback, key=" + info.vkCode);
                        if (info.vkCode == 81) {
                            quit = true;
                        }
                }
            }
            Pointer ptr = info.getPointer();
            long peer = Pointer.nativeValue(ptr);
            return lib.CallNextHookEx(hhk, nCode, wParam, new LPARAM(peer));
        }
    };
    hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
    System.out.println("Keyboard hook installed, type anywhere, 'q' to quit");
    new Thread() {

        @Override
        public void run() {
            while (!quit) {
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                }
            }
            System.err.println("unhook and exit");
            lib.UnhookWindowsHookEx(hhk);
            System.exit(0);
        }
    }.start();
    // This bit never returns from GetMessage
    int result;
    MSG msg = new MSG();
    while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
        if (result == -1) {
            System.err.println("error in get message");
            break;
        } else {
            System.err.println("got message");
            lib.TranslateMessage(msg);
            lib.DispatchMessage(msg);
        }
    }
    lib.UnhookWindowsHookEx(hhk);
}
Also used : MSG(com.sun.jna.platform.win32.WinUser.MSG) KBDLLHOOKSTRUCT(com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) LowLevelKeyboardProc(com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) User32(com.sun.jna.platform.win32.User32)

Aggregations

LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)7 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)7 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)7 Pointer (com.sun.jna.Pointer)4 HWND (com.sun.jna.platform.win32.WinDef.HWND)4 Test (org.junit.Test)3 DesktopWindow (com.sun.jna.platform.DesktopWindow)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)2 COPYDATASTRUCT (com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)2 MSG (com.sun.jna.platform.win32.WinUser.MSG)2 ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)1 User32 (com.sun.jna.platform.win32.User32)1 HICON (com.sun.jna.platform.win32.WinDef.HICON)1 HINSTANCE (com.sun.jna.platform.win32.WinDef.HINSTANCE)1 HHOOK (com.sun.jna.platform.win32.WinUser.HHOOK)1 HOOKPROC (com.sun.jna.platform.win32.WinUser.HOOKPROC)1 KBDLLHOOKSTRUCT (com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT)1 LowLevelKeyboardProc (com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc)1 WNDCLASSEX (com.sun.jna.platform.win32.WinUser.WNDCLASSEX)1