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);
}
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");
}
}
}
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();
}
}
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());
}
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));
}
Aggregations