0
我得到x86程序(在C:\ Program Files(x86))中所有需要的信息,沒有任何下面的語句問題。但對於活動窗口進程名稱的x64空結果。是否有可能檢測出與jna的x64處理器的活動窗口進程名稱(.exe路徑)?jna x64的活動窗口進程名稱
- Eclipse的月神針對x64
- 的Windows 8專業版64位
- JRE8 64
- JNA 4.1.0
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);
}
}
['GetWindowThreadProcessId'](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v = vs.85 ).aspx)需要一個指向32位內存的指針。你給它一個指向一個指針的指針,這個指針是32位或者64位,這取決於你的平臺。然後,您將64位值傳遞給['OpenProcess'](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684320(v = vs.85).aspx),需要一個32位的值。 – technomage 2014-10-06 10:26:57