I have the following interface :
public interface User32 extends W32APIOptions {
User32 instance = (User32) Native.loadLibrary("user32", User32.class,
DEFAULT_OPTIONS);
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
void GetWindowText(int h, StringBuilder s, int nMaxCount);
}
I'm trying to list all the titles of the windows that are currently visible:
final User32 user32 = User32.instance;
user32.EnumWindows(new WNDENUMPROC() {
@Override
public boolean callback(HWND hWnd, Pointer data) {
LOG.debug("callback {}", hWnd);
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText);
LOG.debug("FOUND! {} {}", hWnd, wText);
return true;
}
}, null);
This code works fine on Windows 7 (Apache v6.0, jre6), and I have all the windows listed.
But the same war doesn't work on Windows 8 (Apache v6.0, jre6). I get some weired titles ("theAwtToolkitWindow", "internel window", "Wmi provider host",...) but never the windows that I'm actually looking for.
Any idea why, and how to fix it ?
--== EDIT ==--
Found here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633497%28v=vs.85%29.aspx
Note For Windows 8 and later,
EnumWindows enumerates only top-level windows of desktop apps.
So I guess I'll need to to go to lower-level windows.. but how ?
--== EDIT ==--
I tried to go deeper:
user32.EnumWindows(new MyWNDENUMPROC(0, 3), null);
public class MyWNDENUMPROC implements WNDENUMPROC {
public int level = 0;
public int maxLevel;
public MyWNDENUMPROC(int level, int maxLevel) {
this.level = level;
this.maxLevel = maxLevel;
}
@Override
public boolean callback(HWND hWnd, Pointer data) {
if (maxLevel < level) { //Max level reached..
return false;
}
// Enu les child windows:
user32.EnumChildWindows(hWnd, new MyWNDENUMPROC(level + 1, maxLevel), null);
byte[] windowText = new byte[512];
user32.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText);
if (wText != null && wText.length() > 0) {
LOG.debug("level {} wText {}", level, wText);
}
return true;
}
}
In Windows 7 it goes indeed to deepesr levels, But in windows 8 it stays as level 0.