Search in sources :

Example 6 with User32

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

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

the class PsapiTest method testGetModuleFileNameEx.

@Test
public void testGetModuleFileNameEx() {
    final JFrame w = new JFrame();
    try {
        w.setVisible(true);
        final String searchSubStr = "\\bin\\java";
        final HWND hwnd = new HWND(Native.getComponentPointer(w));
        final IntByReference pid = new IntByReference();
        User32.INSTANCE.GetWindowThreadProcessId(hwnd, pid);
        final HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010, false, pid.getValue());
        if (process == null)
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        // check ANSI function
        final byte[] filePathAnsi = new byte[1025];
        int length = Psapi.INSTANCE.GetModuleFileNameExA(process, null, filePathAnsi, filePathAnsi.length - 1);
        if (length == 0)
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        assertTrue("Path didn't contain '" + searchSubStr + "': " + Native.toString(filePathAnsi), Native.toString(filePathAnsi).toLowerCase().contains(searchSubStr));
        // check Unicode function
        final char[] filePathUnicode = new char[1025];
        length = Psapi.INSTANCE.GetModuleFileNameExW(process, null, filePathUnicode, filePathUnicode.length - 1);
        if (length == 0)
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        assertTrue("Path didn't contain '" + searchSubStr + "': " + Native.toString(filePathUnicode), Native.toString(filePathUnicode).toLowerCase().contains(searchSubStr));
        // check default function
        final int memAllocSize = 1025 * Native.WCHAR_SIZE;
        final Memory filePathDefault = new Memory(memAllocSize);
        length = Psapi.INSTANCE.GetModuleFileNameEx(process, null, filePathDefault, (memAllocSize / Native.WCHAR_SIZE) - 1);
        if (length == 0)
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        assertTrue("Path didn't contain '" + searchSubStr + "': " + Native.toString(filePathDefault.getCharArray(0, memAllocSize / Native.WCHAR_SIZE)), Native.toString(filePathDefault.getCharArray(0, memAllocSize / Native.WCHAR_SIZE)).toLowerCase().contains(searchSubStr));
    } finally {
        w.dispose();
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) JFrame(javax.swing.JFrame) Memory(com.sun.jna.Memory) HWND(com.sun.jna.platform.win32.WinDef.HWND) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

Example 8 with User32

use of com.sun.jna.platform.win32.User32 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 9 with User32

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

the class User32Test method testGetMonitorInfo.

@Test
public final void testGetMonitorInfo() {
    HMONITOR hMon = User32.INSTANCE.MonitorFromWindow(User32.INSTANCE.GetDesktopWindow(), WinUser.MONITOR_DEFAULTTOPRIMARY);
    assertTrue(User32.INSTANCE.GetMonitorInfo(hMon, new MONITORINFO()).booleanValue());
    assertTrue(User32.INSTANCE.GetMonitorInfo(hMon, new MONITORINFOEX()).booleanValue());
}
Also used : HMONITOR(com.sun.jna.platform.win32.WinUser.HMONITOR) MONITORINFOEX(com.sun.jna.platform.win32.WinUser.MONITORINFOEX) MONITORINFO(com.sun.jna.platform.win32.WinUser.MONITORINFO) Test(org.junit.Test)

Example 10 with User32

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

Aggregations

Test (org.junit.Test)21 HWND (com.sun.jna.platform.win32.WinDef.HWND)20 HDC (com.sun.jna.platform.win32.WinDef.HDC)10 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)8 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)8 Pointer (com.sun.jna.Pointer)7 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)7 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)7 RECT (com.sun.jna.platform.win32.WinDef.RECT)6 PIXELFORMATDESCRIPTOR (com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR)6 BufferedImage (java.awt.image.BufferedImage)5 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)4 HICON (com.sun.jna.platform.win32.WinDef.HICON)4 POINT (com.sun.jna.platform.win32.WinDef.POINT)4 PointerByReference (com.sun.jna.ptr.PointerByReference)4 File (java.io.File)4 Memory (com.sun.jna.Memory)3 DesktopWindow (com.sun.jna.platform.DesktopWindow)3 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)3 HBITMAP (com.sun.jna.platform.win32.WinDef.HBITMAP)3