/** * @param process NiFi Process Reference * @param logger Logger Reference for Debug * @return Returns pid or null in-case pid could not be determined * This method takes {@link Process} and {@link Logger} and returns * the platform specific Handle for Win32 Systems, a.k.a <b>pid</b> * In-case it fails to determine the pid, it will return Null. * Purpose for the Logger is to log any interaction for debugging. */ private static Long getWindowsProcessId(final Process process, final Logger logger) { /* determine the pid on windows plattforms */ try { Field f = process.getClass().getDeclaredField("handle"); f.setAccessible(true); long handl = f.getLong(process); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer(Pointer.createConstant(handl)); int ret = kernel.GetProcessId(handle); logger.debug("Detected pid: {}", ret); return Long.valueOf(ret); } catch (final IllegalAccessException | NoSuchFieldException nsfe) { logger.debug("Could not find PID for child process due to {}", nsfe); } return null; }
static long getWindowsPid(Process process) { if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) { try { Field f = process.getClass().getDeclaredField("handle"); f.setAccessible(true); long handl = f.getLong(process); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer(Pointer.createConstant(handl)); return kernel.GetProcessId(handle); } catch (Throwable ignored) { } } return -1; }
try { Process p = Runtime.getRuntime.exec("notepad"); Field f = p.getClass().getDeclaredField("handle"); f.setAccessible(true); long handLong = f.getLong(p); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer(Pointer.createConstant(handLong)); int pid = kernel.GetProcessId(handle); System.out.print(pid); } catch (Throwable e) { }
/** * Constructor for the AutomationApplication. * * @param builder The builder */ public AutomationApplication(final ElementBuilder builder) { super(builder); this.isAttached = builder.getAttached(); this.user32 = builder.getUser32(); this.process = builder.getProcess(); // Process process = builder.getProcess(); if (builder.getHasHandle()) { this.handle = builder.getHandle(); } else { String name = process.getClass().getName(); if (name.equals("java.lang.Wind32Process") || name.equals("java.lang.ProcessImpl")) { try { //noinspection JavaReflectionMemberAccess Field f = process.getClass().getDeclaredField("handle"); f.setAccessible(true); this.handle.setPointer(Pointer.createConstant( f.getLong(process))); } catch (Throwable e) { // Handle the error nicely } } } }
/** * @see http://www.golesny.de/p/code/javagetpid * * @return */ private static Long windowsProcessId(Process process) { if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) { /* determine the pid on windows plattforms */ try { Field f = process.getClass().getDeclaredField("handle"); f.setAccessible(true); long handl = f.getLong(process); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer(Pointer.createConstant(handl)); int ret = kernel.GetProcessId(handle); logger.debug("Detected pid: {}", ret); return Long.valueOf(ret); } catch (Throwable e) { logger.debug("Could not determine pid", e); } } return null; }
/** * @see http://www.golesny.de/p/code/javagetpid * * @return */ private static Long windowsProcessId(Process process) { if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) { /* determine the pid on windows plattforms */ try { Field f = process.getClass().getDeclaredField("handle"); f.setAccessible(true); long handl = f.getLong(process); Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer(Pointer.createConstant(handl)); int ret = kernel.GetProcessId(handle); logger.debug("Detected pid: {}", ret); return (long) ret; } catch (Throwable e) { e.printStackTrace(); } } return null; }
//Imports import com.sun.jna.*; import com.sun.jna.platform.win32.Kernel32; import com.sun.jna.platform.win32.WinNT; private String getWindowsProcessId(Process proc) { if (proc.getClass().getName().equals("java.lang.Win32Process") || proc.getClass().getName().equals("java.lang.ProcessImpl")) { try { 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)); return Integer.toString(kernel.GetProcessId(handle)); } catch (Throwable e) { } } return ""; }
private Integer getProcessId() { try { if (this.process == null) return null; if (CommonUtils.isUnix()) { Field field = ReflectUtils.getField(this.process.getClass(), "pid"); if (field!=null) { return field.getInt(this.process); } }else if(CommonUtils.isWindows()) { Field field = ReflectUtils.getField(this.process.getClass(), "handle"); if (field!=null) { WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer(Pointer.createConstant(field.getLong(this.process))); return Kernel32.INSTANCE.GetProcessId(handle); } } }catch (Exception e) { e.printStackTrace(); } return null; }
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; }
private static int getWindowsPid(final Process p) { Field f; long lHandle; try { f = p.getClass().getDeclaredField("handle"); f.setAccessible(true); lHandle = f.getLong(p); } catch (NoSuchFieldException | SecurityException | IllegalAccessException ex) { throw new UncheckedExecutionException("Cannot get PID for " + p, ex); } Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer(Pointer.createConstant(lHandle)); return kernel.GetProcessId(handle); }
private static int getWindowsPid(final Process p) { Field f; long lHandle; try { f = p.getClass().getDeclaredField("handle"); f.setAccessible(true); lHandle = f.getLong(p); } catch (NoSuchFieldException | SecurityException | IllegalAccessException ex) { throw new UncheckedExecutionException("Cannot get PID for " + p, ex); } Kernel32 kernel = Kernel32.INSTANCE; WinNT.HANDLE handle = new WinNT.HANDLE(); handle.setPointer(Pointer.createConstant(lHandle)); return kernel.GetProcessId(handle); }
public HANDLE getValue() { Pointer p = getPointer().getPointer(0); if (p == null) { return null; } if (WinBase.INVALID_HANDLE_VALUE.getPointer().equals(p)) { return WinBase.INVALID_HANDLE_VALUE; } HANDLE h = new HANDLE(); h.setPointer(p); return h; } }
public HANDLE(Pointer p) { setPointer(p); immutable = true; }
public HANDLE(Pointer p) { setPointer(p); immutable = true; }
@Nonnull protected HANDLE toHandle(long handleId) { final HANDLE handle = new HANDLE(); handle.setPointer(Pointer.createConstant(handleId)); return handle; }
public HANDLE getValue() { Pointer p = getPointer().getPointer(0); if (p == null) { return null; } if (WinBase.INVALID_HANDLE_VALUE.getPointer().equals(p)) { return WinBase.INVALID_HANDLE_VALUE; } HANDLE h = new HANDLE(); h.setPointer(p); return h; } }