Problem Using JNA and EnumWindows

I am experimenting with JNA and this is the first program I am trying to run. I copied it from the link, but when I started it, it found 412 windows ... and I’m sure that I don’t have many windows open. Maybe someone will explain to me the behavior of the program?

import com.sun.jna.Pointer; import com.sun.jna.win32.StdCallLibrary.StdCallCallback; import com.sun.jna.Native; import com.sun.jna.win32.StdCallLibrary; public class Main { // Equivalent JNA mappings public interface User32 extends StdCallLibrary { User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class); interface WNDENUMPROC extends StdCallCallback { boolean callback(Pointer hWnd, Pointer arg); } boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg); } public static void main(String[] args) { User32 user32 = User32.INSTANCE; user32.EnumWindows(new User32.WNDENUMPROC() { int count; public boolean callback(Pointer hWnd, Pointer userData) { System.out.println("Found window " + hWnd + ", total " + ++count); return true; } }, null); } } 
+4
source share
1 answer

On Windows, almost everything is a Window. Here are some changes to your code that will display some window titles / text:

 import com.sun.jna.Pointer; import com.sun.jna.Native; import com.sun.jna.win32.StdCallLibrary; public class JNA_Main { // Equivalent JNA mappings public interface User32 extends StdCallLibrary { User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class); interface WNDENUMPROC extends StdCallCallback { boolean callback(Pointer hWnd, Pointer arg); } boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg); int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount); } public static void main(String[] args) { final User32 user32 = User32.INSTANCE; user32.EnumWindows(new User32.WNDENUMPROC() { int count; public boolean callback(Pointer hWnd, Pointer userData) { byte[] windowText = new byte[512]; user32.GetWindowTextA(hWnd, windowText, 512); String wText = Native.toString(windowText); wText = (wText.isEmpty()) ? "" : "; text: " + wText; System.out.println("Found window " + hWnd + ", total " + ++count + wText); return true; } }, null); } } 

Please ask if something is incomprehensible.

+7
source

Source: https://habr.com/ru/post/1332606/


All Articles