Skip to content

Commit

Permalink
java - get process id in a way that works cross-platform
Browse files Browse the repository at this point in the history
  • Loading branch information
cgoldberg authored and lukeis committed Sep 26, 2016
1 parent 1c52dc8 commit b3c53bf
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions java/client/src/org/openqa/selenium/os/ProcessUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,28 @@ private static void closeAllStreamsAndDestroyProcess(Process process) {
}

static int getProcessId(Process p) {
if (Platform.getCurrent().is(WINDOWS)) {
throw new IllegalStateException("UnixUtils may not be used on Windows");
}
try {
Field f = p.getClass().getDeclaredField("pid");
f.setAccessible(true);
return (Integer) f.get(p);
} catch (Exception e) {
throw new RuntimeException("Couldn't detect pid", e);
if (thisIsWindows()) {
try {
Field f = p.getClass().getDeclaredField("handle");
f.setAccessible(true);
long hndl = f.getLong(p);

Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE handle = new WinNT.HANDLE();
handle.setPointer(Pointer.createConstant(hndl));
int pid = kernel.GetProcessId(handle);
return pid;
} catch (Exception e) {
throw new RuntimeException("Couldn't detect pid", e);
}
} else {
try {
Field f = p.getClass().getDeclaredField("pid");
f.setAccessible(true);
return (Integer) f.get(p);
} catch (Exception e) {
throw new RuntimeException("Couldn't detect pid", e);
}
}
}

Expand Down

0 comments on commit b3c53bf

Please sign in to comment.