我获得x86程序的所有必需信息(在C:\ Program Files(x86)中),而不会出现以下语句的任何问题。但是对于激活窗口进程名称的x64空结果。是否可以使用jna检测x64处理器的活动窗口进程名称(.exe路径)?
import static your.pack.name.EnumerateWindows.Kernel32.OpenProcess;
import static your.pack.name.EnumerateWindows.Kernel32.PROCESS_QUERY_INFORMATION;
import static your.pack.name.EnumerateWindows.Kernel32.PROCESS_VM_READ;
import static your.pack.name.EnumerateWindows.Psapi.GetModuleBaseNameW;
import static your.pack.name.EnumerateWindows.User32DLL.GetForegroundWindow;
import static your.pack.name.EnumerateWindows.User32DLL.GetWindowTextW;
import static your.pack.name.EnumerateWindows.User32DLL.GetWindowThreadProcessId;
import java.lang.management.ManagementFactory;
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));
System.out.println(ManagementFactory.getRuntimeMXBean().getName());
}
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);
}
}
答案 0 :(得分:0)
如果我将项目导出到runnable jar,我可以毫无问题地获取x86和x64软件的所有信息。 如果我从Eclipse运行项目
,上述问题仅影响