我正在尝试使用Java升级我的应用程序,只有在具有特定名称的进程窗口处于活动状态时才能工作。我发现使用JNI可以实现这一点,但我不知道究竟是怎么做到的。我找不到任何可以解释它的描述或例子。我的问题是 - 如何在Windows中获取当前活动窗口的进程名称(通过JNI或其他任何方式 - 我接受任何其他解决方案)?
答案 0 :(得分:55)
为自己节省一些痛苦并使用JNA。对于Win32 API,您需要download jna.jar 和 jna-platform.jar 。 pinvoke wiki和MSDN对于查找正确的系统调用非常有用。
无论如何,这是打印当前活动窗口的标题和过程的代码。
import static enumeration.EnumerateWindows.Kernel32.*;
import static enumeration.EnumerateWindows.Psapi.*;
import static enumeration.EnumerateWindows.User32DLL.*;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.ptr.PointerByReference;
public class EnumerateWindows {
private static final int MAX_TITLE_LENGTH = 1024;
public static void main(String[] args) throws Exception {
char[] buffer = new char[MAX_TITLE_LENGTH * 2];
GetWindowTextW(GetForegroundWindow(), buffer, MAX_TITLE_LENGTH);
System.out.println("Active window title: " + Native.toString(buffer));
PointerByReference pointer = new PointerByReference();
GetWindowThreadProcessId(GetForegroundWindow(), pointer);
Pointer process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pointer.getValue());
GetModuleBaseNameW(process, null, buffer, MAX_TITLE_LENGTH);
System.out.println("Active window process: " + Native.toString(buffer));
}
static class Psapi {
static { Native.register("psapi"); }
public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
}
static class Kernel32 {
static { Native.register("kernel32"); }
public static int PROCESS_QUERY_INFORMATION = 0x0400;
public static int PROCESS_VM_READ = 0x0010;
public static native int GetLastError();
public static native Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, Pointer pointer);
}
static class User32DLL {
static { Native.register("user32"); }
public static native int GetWindowThreadProcessId(HWND hWnd, PointerByReference pref);
public static native HWND GetForegroundWindow();
public static native int GetWindowTextW(HWND hWnd, char[] lpString, int nMaxCount);
}
}
答案 1 :(得分:9)
此代码适用于JNA 4.0
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
// see http://java-native-access.github.io/jna/4.0/javadoc/
public class EnumerateWindows {
private static final int MAX_TITLE_LENGTH = 1024;
public static void main(String[] args) throws Exception {
char[] buffer = new char[MAX_TITLE_LENGTH * 2];
HWND hwnd = User32.INSTANCE.GetForegroundWindow();
User32.INSTANCE.GetWindowText(hwnd, buffer, MAX_TITLE_LENGTH);
System.out.println("Active window title: " + Native.toString(buffer));
RECT rect = new RECT();
User32.INSTANCE.GetWindowRect(hwnd, rect);
System.out.println("rect = " + rect);
}
}
答案 2 :(得分:0)
简单
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.StdCallLibrary;
public class WinSysUser32HWND {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
HWND GetForegroundWindow();
void GetWindowTextW(HWND hWnd, char[] lpString, int nMaxCount);
}
public static void main(String[] args) {
char[] buffer = new char[1024 * 2];
User32.INSTANCE.GetWindowTextW(User32.INSTANCE.GetForegroundWindow(), buffer, 1024);
System.out.println("Active window title: " + Native.toString(buffer));
}
}