Search in sources :

Example 1 with HWND

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

the class User32Test method testGetDesktopWindow.

@Test
public void testGetDesktopWindow() {
    HWND desktopWindow = User32.INSTANCE.GetDesktopWindow();
    assertNotNull("Failed to get desktop window HWND", desktopWindow);
}
Also used : HWND(com.sun.jna.platform.win32.WinDef.HWND) Test(org.junit.Test)

Example 2 with HWND

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

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

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

the class User32Test method testGetActiveWindow.

@Test
public void testGetActiveWindow() {
    HWND result = User32.INSTANCE.GetActiveWindow();
    assertNull("GetActiveWindow result should be null", result);
    assertEquals("GetLastError should be ERROR_SUCCESS.", WinError.ERROR_SUCCESS, Native.getLastError());
}
Also used : HWND(com.sun.jna.platform.win32.WinDef.HWND) Test(org.junit.Test)

Example 5 with HWND

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

the class User32Test method testMonitorFromWindow.

@Test
public final void testMonitorFromWindow() {
    int dwFlags = WinUser.MONITOR_DEFAULTTOPRIMARY;
    HWND hwnd = new HWND();
    assertNotNull(User32.INSTANCE.MonitorFromWindow(hwnd, dwFlags));
}
Also used : HWND(com.sun.jna.platform.win32.WinDef.HWND) Test(org.junit.Test)

Aggregations

HWND (com.sun.jna.platform.win32.WinDef.HWND)29 HDC (com.sun.jna.platform.win32.WinDef.HDC)9 Pointer (com.sun.jna.Pointer)7 Test (org.junit.Test)7 PIXELFORMATDESCRIPTOR (com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR)6 BufferedImage (java.awt.image.BufferedImage)6 JFrame (javax.swing.JFrame)6 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)4 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)4 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)4 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)4 Memory (com.sun.jna.Memory)3 HBITMAP (com.sun.jna.platform.win32.WinDef.HBITMAP)3 RECT (com.sun.jna.platform.win32.WinDef.RECT)3 BITMAPINFO (com.sun.jna.platform.win32.WinGDI.BITMAPINFO)3 Rectangle (java.awt.Rectangle)3 COPYDATASTRUCT (com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)2 IntByReference (com.sun.jna.ptr.IntByReference)2 PointerByReference (com.sun.jna.ptr.PointerByReference)2 File (java.io.File)2