@Test public void getPIDWhenProcessExists() { int pid = -1; Process process = null; List<String> cmds = new ArrayList<>(); cmds.add( "cmd.exe" ); ProcessBuilder processBuilder = new ProcessBuilder( cmds ); try { process = processBuilder.start(); pid = WinProcess.getPID( process ); } catch ( IOException e ) { e.printStackTrace(); } finally { process.destroy(); } Assert.assertTrue( pid > 0 ); Assert.assertFalse( process.isAlive() ); }
public static int getPID( Process proc ) { int pid = -1; try { if ( proc.getClass().getName().equals( "java.lang.Win32Process" ) || proc.getClass().getName() .equals( "java.lang.ProcessImpl" ) ) { Field f = proc.getClass().getDeclaredField( "handle" ); f.setAccessible( true ); long handl = f.getLong( proc ); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer( Pointer.createConstant( handl ) ); pid = kernel.GetProcessId( handle ); } } catch ( Exception e ) { e.printStackTrace(); } return pid; }
WinProcess { public static int getPID( Process proc ) { int pid = -1; try { if ( proc.getClass().getName().equals( "java.lang.Win32Process" ) || proc.getClass().getName() .equals( "java.lang.ProcessImpl" ) ) { Field f = proc.getClass().getDeclaredField( "handle" ); f.setAccessible( true ); long handl = f.getLong( proc ); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer( Pointer.createConstant( handl ) ); pid = kernel.GetProcessId( handle ); } } catch ( Exception e ) { e.printStackTrace(); } return pid; } }
WinProcess { public static int getPID( Process proc ) { int pid = -1; try { if ( proc.getClass().getName().equals( "java.lang.Win32Process" ) || proc.getClass().getName() .equals( "java.lang.ProcessImpl" ) ) { Field f = proc.getClass().getDeclaredField( "handle" ); f.setAccessible( true ); long handl = f.getLong( proc ); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer( Pointer.createConstant( handl ) ); pid = kernel.GetProcessId( handle ); } } catch ( Exception e ) { e.printStackTrace(); } return pid; } WinProcess( int pid ); }
WinProcess { public static int getPID( Process proc ) { int pid = -1; try { if ( proc.getClass().getName().equals( "java.lang.Win32Process" ) || proc.getClass().getName() .equals( "java.lang.ProcessImpl" ) ) { Field f = proc.getClass().getDeclaredField( "handle" ); f.setAccessible( true ); long handl = f.getLong( proc ); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer( Pointer.createConstant( handl ) ); pid = kernel.GetProcessId( handle ); } } catch ( Exception e ) { e.printStackTrace(); } return pid; } WinProcess( int pid ); void terminate(); String killChildProcesses(); static int getPID( Process proc ); int getWinProcessPID(); }
WinProcess { public static int getPID( Process proc ) { int pid = -1; try { if ( proc.getClass().getName().equals( "java.lang.Win32Process" ) || proc.getClass().getName() .equals( "java.lang.ProcessImpl" ) ) { Field f = proc.getClass().getDeclaredField( "handle" ); f.setAccessible( true ); long handl = f.getLong( proc ); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer( Pointer.createConstant( handl ) ); pid = kernel.GetProcessId( handle ); } } catch ( Exception e ) { e.printStackTrace(); } return pid; } WinProcess( int pid ); void terminate(); String killChildProcesses(); static int getPID( Process proc ); int getWinProcessPID();  }