/** * @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; }
if (eventHandle.getPointer().getInt(0) == WEvtApi.EvtSubscribeErrors.ERROR_EVT_QUERY_RESULT_STALE) { logger.error(MISSING_EVENT_MESSAGE); } else { logger.error(RECEIVED_THE_FOLLOWING_WIN32_ERROR + eventHandle.getPointer().getInt(0));
if (result == null || WinGDI.HGDI_ERROR.equals(result)) { Win32Exception ex = new Win32Exception(Native.getLastError()); if (we != null) {
WinNT.HANDLE[] result = new WinNT.HANDLE[rawValue.length]; for (int i = 0; i < result.length; i++) { result[i] = new WinNT.HANDLE(rawValue[i]); return new WinNT.HANDLE(field1.getPointer().getPointer(0));
mask, null, WinNT.OPEN_EXISTING, flags, null); if (WinBase.INVALID_HANDLE_VALUE.equals(handle)) { throw new IOException("Unable to open " + file + " (" + klib.GetLastError() + ")"); handleMap.put(handle, finfo); port = klib.CreateIoCompletionPort(handle, port, handle.getPointer(), 0); if (WinBase.INVALID_HANDLE_VALUE.equals(port)) { throw new IOException("Unable to create/use I/O Completion port " + "for " + file + " ("
mask, null, WinNT.OPEN_EXISTING, flags, null); if (WinBase.INVALID_HANDLE_VALUE.equals(handle)) { throw new IOException("Unable to open " + file + " (" + klib.GetLastError() + ")"); handleMap.put(handle, finfo); port = klib.CreateIoCompletionPort(handle, port, handle.getPointer(), 0); if (WinBase.INVALID_HANDLE_VALUE.equals(port)) { throw new IOException("Unable to create/use I/O Completion port " + "for " + file + " ("
new HANDLEByReference().getValue()); if (WinBase.INVALID_HANDLE_VALUE.equals(hFile)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
} finally { HANDLE hToken = phToken.getValue(); if (!WinBase.INVALID_HANDLE_VALUE.equals(hToken)) { try { Kernel32Util.closeHandle(hToken);
if (eventHandle.getPointer().getInt(0) == WEvtApi.EvtSubscribeErrors.ERROR_EVT_QUERY_RESULT_STALE) { logger.error(MISSING_EVENT_MESSAGE); } else { logger.error(RECEIVED_THE_FOLLOWING_WIN32_ERROR + eventHandle.getPointer().getInt(0));
new HANDLEByReference().getValue()); if (WinBase.INVALID_HANDLE_VALUE.equals(hFile)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
/** * 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) ret; } catch (Throwable e) { e.printStackTrace(); } } 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.valueOf(ret); } catch (Throwable e) { logger.debug("Could not determine pid", e); } } return null; }
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; }
//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 ""; }
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; }
public void setValue(HANDLE h) { getPointer().setPointer(0, h != null ? h.getPointer() : 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; }
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); }