@Test public void openedProcessHandleClosesItselfAfterTryWithResourcesBlock() throws Exception { int nativeHandle = 123; openProcess = (flags, inherit, pid) -> nativeHandle; List<Integer> closedHandles = new ArrayList<>(); closeHandle = closedHandles::add; try (ProcessHandle handle = opener.open(1555, ProcessAccessFlags.ALL)) { assertEquals(nativeHandle, handle.getNativeHandle()); } assertTrue( "ProcessHandle didn't close after the try-with-resources block!", closedHandles.contains(nativeHandle)); }
@Override public ProcessHandle open(int pid, int processAccessFlags) throws ProcessOpenException { int handle = openProcess.openProcess(processAccessFlags, false, pid); if (handle == WinApiConstants.NULL) { Integer lastError = Native.getLastError(); SystemErrorCode error = SystemErrorCode.fromId(lastError); String message = String.format( "Failed to open process %d for reading! System error: %s", pid, error == null ? lastError.toString() : error.toString() ); throw new ProcessOpenException(message); } return new WinApiProcessHandle(handle); }
WindowsProcessOpener implements ProcessOpener { @Override public ProcessHandle open(int pid, int processAccessFlags) throws ProcessOpenException { int handle = openProcess.openProcess(processAccessFlags, false, pid); if (handle == WinApiConstants.NULL) { Integer lastError = Native.getLastError(); SystemErrorCode error = SystemErrorCode.fromId(lastError); String message = String.format( "Failed to open process %d for reading! System error: %s", pid, error == null ? lastError.toString() : error.toString() ); throw new ProcessOpenException(message); } return new WinApiProcessHandle(handle); } }
WindowsProcessOpener implements ProcessOpener { @Override public ProcessHandle open(int pid, int processAccessFlags) throws ProcessOpenException { int handle = openProcess.openProcess(processAccessFlags, false, pid); if (handle == WinApiConstants.NULL) { Integer lastError = Native.getLastError(); SystemErrorCode error = SystemErrorCode.fromId(lastError); String message = String.format( "Failed to open process %d for reading! System error: %s", pid, error == null ? lastError.toString() : error.toString() ); throw new ProcessOpenException(message); } return new WinApiProcessHandle(handle); } <T extends OpenProcess & CloseHandle> WindowsProcessOpener(T processOpenerAndHandleCloser);   WindowsProcessOpener(OpenProcess openProcess, CloseHandle closeHandle); }
WindowsProcessOpener implements ProcessOpener { @Override public ProcessHandle open(int pid, int processAccessFlags) throws ProcessOpenException { int handle = openProcess.openProcess(processAccessFlags, false, pid); if (handle == WinApiConstants.NULL) { Integer lastError = Native.getLastError(); SystemErrorCode error = SystemErrorCode.fromId(lastError); String message = String.format( "Failed to open process %d for reading! System error: %s", pid, error == null ? lastError.toString() : error.toString() ); throw new ProcessOpenException(message); } return new WinApiProcessHandle(handle); } <T extends OpenProcess & CloseHandle> WindowsProcessOpener(T processOpenerAndHandleCloser);   WindowsProcessOpener(OpenProcess openProcess, CloseHandle closeHandle); @Override ProcessHandle open(int pid, int processAccessFlags); }
WindowsProcessOpener implements ProcessOpener { @Override public ProcessHandle open(int pid, int processAccessFlags) throws ProcessOpenException { int handle = openProcess.openProcess(processAccessFlags, false, pid); if (handle == WinApiConstants.NULL) { Integer lastError = Native.getLastError(); SystemErrorCode error = SystemErrorCode.fromId(lastError); String message = String.format( "Failed to open process %d for reading! System error: %s", pid, error == null ? lastError.toString() : error.toString() ); throw new ProcessOpenException(message); } return new WinApiProcessHandle(handle); } <T extends OpenProcess & CloseHandle> WindowsProcessOpener(T processOpenerAndHandleCloser);   WindowsProcessOpener(OpenProcess openProcess, CloseHandle closeHandle); @Override ProcessHandle open(int pid, int processAccessFlags);  }