@Override public int getProcessId() { return Kernel32.INSTANCE.GetCurrentProcessId(); }
int intResult = 0; process = kernel32.GetCurrentProcessId(); status = ehlapi32.WD_ConnectPS(process, "A"); intResult = ehlapi32.WD_SendKey(process, "MYNAME");
/** * Factory method to avoid loading JNA classes every connection. * * @return factory that implement PID according to environment. */ public static PidRequestInter getInstance() { try { if (Platform.isLinux()) { //Linux pid implementation return () -> String.valueOf(CLibrary.INSTANCE.getpid()); } else { if (Platform.isWindows()) { //Windows pid implementation return () -> { try { return String.valueOf(Kernel32.INSTANCE.GetCurrentProcessId()); } catch (Throwable cle) { //jna plateform jar's are not in classpath, no PID returned } return null; }; } } } catch (Throwable cle) { //jna jar's are not in classpath, no PID returned } //No JNA, or environment not Linux/windows -> return no PID return () -> null; }
/** * Factory method to avoid loading JNA classes every connection. * * @return factory that implement PID according to environment. */ public static PidRequestInter getInstance() { try { if (Platform.isLinux()) { //Linux pid implementation return () -> String.valueOf(CLibrary.INSTANCE.getpid()); } else { if (Platform.isWindows()) { //Windows pid implementation return () -> { try { return String.valueOf(Kernel32.INSTANCE.GetCurrentProcessId()); } catch (Throwable cle) { //jna plateform jar's are not in classpath, no PID returned } return null; }; } } } catch (Throwable cle) { //jna jar's are not in classpath, no PID returned } //No JNA, or environment not Linux/windows -> return no PID return () -> null; }
/** * Returns the current process id. * * @return the current process id */ public static int getPID() { int result; if (isWindows()) { result = Kernel32.INSTANCE.GetCurrentProcessId(); } else if (isLinux() || isMacOSX()) { return CLibrary.INSTANCE.getpid(); } else { System.out.println(">> Runing on unsupported OS." + " Assuming POSIX compliant OS."); return CLibrary.INSTANCE.getpid(); } return result; }
import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.ptr.IntByReference; import com.sun.jna.win32.*; import com.sun.jna.examples.win32.Kernel32; ... public static boolean isLocalSession() { Kernel32 kernel32; IntByReference pSessionId; int consoleSessionId; Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); pSessionId = new IntByReference(); if (lib.ProcessIdToSessionId(lib.GetCurrentProcessId(), pSessionId)) { consoleSessionId = lib.WTSGetActiveConsoleSessionId(); return (consoleSessionId != 0xFFFFFFFF && consoleSessionId == pSessionId.getValue()); } else return false; }