public interface Psapi extends StdCallLibrary { Psapi INSTANCE = (Psapi) Native.loadLibrary("Psapi", Psapi.class); WinDef.DWORD GetModuleBaseNameW(Pointer hProcess, Pointer hModule, byte[] lpBaseName, int nSize); } if (Platform.isWindows()) { final int PROCESS_VM_READ=0x0010; final int PROCESS_QUERY_INFORMATION=0x0400; final User32 user32 = User32.INSTANCE; final Kernel32 kernel32=Kernel32.INSTANCE; final Psapi psapi = Psapi.INSTANCE; WinDef.HWND windowHandle=user32.GetForegroundWindow(); IntByReference pid= new IntByReference(); user32.GetWindowThreadProcessId(windowHandle, pid); WinNT.HANDLE processHandle=kernel32.OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, true, pid.getValue()); byte[] filename = new byte[512]; Psapi.INSTANCE.GetModuleBaseNameW(processHandle.getPointer(), Pointer.NULL, filename, filename.length); String name=new String(filename); System.out.println(name); if (name.endsWith("wwahost.exe")) { // Metro App // There is no stable API to get the current Metro app // But you can guestimate the name form the current directory of the process // To query this, see: // http://stackoverflow.com/questions/16110936/read-other-process-current-directory-in-c-sharp }
WinProcess( int pid ) throws IOException { handle = Kernel32.INSTANCE .OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_SUSPEND_RESUME | PROCESS_TERMINATE | PROCESS_SYNCHRONIZE, false, pid ); if ( handle == null ) { throw new IOException( "OpenProcess failed: " + Kernel32Util.formatMessageFromLastErrorCode( Kernel32.INSTANCE.GetLastError() ) ); } this.pid = pid; }
/** * Gets the handle of a process from the process entry. * * @param processEntry The processEntry to use * @return The handle * @throws AutomationException Thrown if the handle cannot be determined */ public static WinNT.HANDLE getHandleFromProcessEntry( final Tlhelp32.PROCESSENTRY32.ByReference processEntry) throws AutomationException { ensureWinApiInstances(); WinNT.HANDLE handle = kernel32.OpenProcess( 0x0400 | /* PROCESS_QUERY_INFORMATION */ 0x0800 | /* PROCESS_SUSPEND_RESUME */ 0x0001 | /* PROCESS_TERMINATE */ 0x00100000 /* SYNCHRONIZE */, false, processEntry.th32ProcessID.intValue()); if (handle == null) { throw new AutomationException("OpenProcess failed"); } return handle; }
/** * Dermines whether the specified process is running. * * @param pid process id * @return <code>true</code> if the specified process is running; * <code>false</code> otherwise */ public static boolean isRunning(int pid) { if (isWindows()) { // TODO why passing 1 works, 0 does not? the documentation clearly // states that this value should be set to zero. return Kernel32.INSTANCE.OpenProcess(1, false, pid) != null; } else if (isLinux() || isMacOSX()) { return CLibrary.INSTANCE.kill(pid, 0) == 0; } else { System.out.println(">> Runing on unsupported OS." + " Assuming POSIX compliant OS."); return CLibrary.INSTANCE.kill(pid, 0) == 0; } }
User32.INSTANCE.GetWindowThreadProcessId(hwnd, pid); final HANDLE process = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_QUERY_INFORMATION | WinNT.PROCESS_VM_READ, false, pid.getValue()); if (process == null) {
Pointer process = kernel32.OpenProcess(permissions, true, pid); return process;