@Override public void dispose() { if (this.windowsIdentity != null) { Kernel32.INSTANCE.CloseHandle(this.windowsIdentity); } }
@Override public void dispose() { if (this.windowsIdentity != null) { Kernel32.INSTANCE.CloseHandle(this.windowsIdentity); } }
@Override public void dispose() { if (this.windowsIdentity != null) { Kernel32.INSTANCE.CloseHandle(this.windowsIdentity); } }
private synchronized void closeUnusedProcessHandle() { // Keep the process handle open until both conditions are met: // 1. The process has exited. // 2. We have disconnected from the agent, by closing the winpty_t // object. // As long as the process handle is open, Windows will not reuse the child // process' PID. // https://blogs.msdn.microsoft.com/oldnewthing/20110107-00/?p=11803 if (myClosed && myChildExited && myProcess != null) { Kernel32.INSTANCE.CloseHandle(myProcess); myProcess = null; } }
protected synchronized void unwatch(File file) { FileInfo finfo = fileMap.remove(file); if (finfo != null) { handleMap.remove(finfo.handle); Kernel32 klib = Kernel32.INSTANCE; // bug: the watcher may still be processing this file klib.CloseHandle(finfo.handle); } }
@Override protected synchronized void unwatch(File file) { FileInfo finfo = fileMap.remove(file); if (finfo != null) { handleMap.remove(finfo.handle); Kernel32 klib = Kernel32.INSTANCE; // bug: the watcher may still be processing this file klib.CloseHandle(finfo.handle); // TODO check error code if failed to close } }
private synchronized boolean closeImpl() { if (shutdownFlag) { // If shutdownFlag is already set, then the handles are already closed. return false; } shutdownFlag = true; Kernel32.INSTANCE.SetEvent(shutdownEvent); if (!myFinalizedFlag) { readLock.lock(); writeLock.lock(); writeLock.unlock(); readLock.unlock(); } Kernel32.INSTANCE.CloseHandle(shutdownEvent); Kernel32.INSTANCE.CloseHandle(readEvent); Kernel32.INSTANCE.CloseHandle(writeEvent); return true; }
public synchronized void dispose() { disposing = true; // unwatch any remaining files in map, allows watcher thread to exit int i = 0; for (Object[] keys = fileMap.keySet().toArray(); !fileMap.isEmpty();) { unwatch((File)keys[i++]); } Kernel32 klib = Kernel32.INSTANCE; klib.PostQueuedCompletionStatus(port, 0, null, null); klib.CloseHandle(port); port = null; watcher = null; } }
@Override public synchronized void dispose() { disposing = true; // unwatch any remaining files in map, allows watcher thread to exit int i = 0; for (Object[] keys = fileMap.keySet().toArray(); !fileMap.isEmpty();) { unwatch((File)keys[i++]); } Kernel32 klib = Kernel32.INSTANCE; klib.PostQueuedCompletionStatus(port, 0, null, null); klib.CloseHandle(port); // TODO check error code if failed to close port = null; watcher = null; } }
/** * Shut down the NamedPipe cleanly and quickly. Use an event to abort any * pending I/O, then acquire the locks to ensure that the I/O has ended. * Once everything has stopped, close all the native handles. * * Mark the function synchronized to ensure that a later call cannot return * earlier. */ public synchronized void close() throws IOException { if (!closeImpl()) { return; } if (!Kernel32.INSTANCE.CloseHandle(myHandle)) { throw new IOException("Close error:" + Native.getLastError()); } }
/** * Invokes {@link Kernel32#CloseHandle(WinNT.HANDLE)} and checks the success code. * If not successful, then throws a {@link Win32Exception} with the * {@code GetLastError} value * * @param h The handle to be closed - ignored if {@code null} */ public static void closeHandle(HANDLE h) { if (h == null) { return; } if (!Kernel32.INSTANCE.CloseHandle(h)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } }
public class windows { public static void main(String[] args) { Kernel32 kernel32 = Kernel32.INSTANCE; WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0)); Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); try { while (kernel32.Process32Next(snapshot, processEntry)) { HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010, false, 0); if (process != null) { int len = psapi.GetModuleFileNameExW(process, null, path, 1024); if (len > 0) System.out.println(new String(path, 0, len)); else System.out.println("GetModuleFileNameW failed"); } else System.out.println("OpenProcess failed"); System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile) + "\t" + processEntry.cntThreads); } } finally { kernel32.CloseHandle(snapshot); } }
private List<WinProcess> getChildProcesses() throws IOException { int childPID; List<WinProcess> processList = new ArrayList<>(); List<Integer> pidList = new ArrayList<>(); pidList.add( pid ); int parentPID; Kernel32 kernel32 = Native.loadLibrary( Kernel32.class, W32APIOptions.UNICODE_OPTIONS ); Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot( Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD( 0 ) ); try { while ( kernel32.Process32Next( snapshot, processEntry ) ) { parentPID = processEntry.th32ParentProcessID.intValue(); if ( pidList.contains( parentPID ) ) { childPID = processEntry.th32ProcessID.intValue(); pidList.add( childPID ); processList.add( new WinProcess( childPID ) ); } } } finally { kernel32.CloseHandle( snapshot ); } return processList; }
import com.sun.jna.platform.win32.Kernel32; import com.sun.jna.platform.win32.Tlhelp32; import com.sun.jna.platform.win32.WinDef; import com.sun.jna.platform.win32.WinNT; import com.sun.jna.win32.W32APIOptions; import com.sun.jna.Native; public class ListProcesses { public static void main(String[] args) { Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS); Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0)); try { while (kernel32.Process32Next(snapshot, processEntry)) { System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile)); } } finally { kernel32.CloseHandle(snapshot); } } }
/** * Finds the given process in the process list. * * @param processEntry The process entry. * @param filenamePattern pattern matching the filename of the process. * @return The found process entry. */ public static boolean findProcessEntry( final Tlhelp32.PROCESSENTRY32.ByReference processEntry, final Pattern filenamePattern) { Kernel32 kern32 = Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS); WinNT.HANDLE snapshot = kern32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0)); boolean found = false; try { while (kern32.Process32Next(snapshot, processEntry)) { String fname = Native.toString(processEntry.szExeFile); if (fname != null && filenamePattern.matcher(fname).matches()) { found = true; break; } } } finally { kern32.CloseHandle(snapshot); } return found; }
import com.sun.jna.platform.win32.Kernel32; import com.sun.jna.platform.win32.Tlhelp32; import com.sun.jna.platform.win32.WinDef; import com.sun.jna.platform.win32.WinNT; import com.sun.jna.win32.W32APIOptions; import com.sun.jna.Native; public class ListProcesses { public static void main(String[] args) { Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS); Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0)); try { while (kernel32.Process32Next(snapshot, processEntry)) { System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile)); } } finally { kernel32.CloseHandle(snapshot); } } }
public boolean jambo_loco (int pid) { Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS); Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0)); try { int i = 0; int size = processEntry.dwSize.intValue(); while (kernel32.Process32Next(snapshot, processEntry) && i < size) { if (processEntry.th32ProcessID.intValue() == pid) return true; i++; } } finally { kernel32.CloseHandle(snapshot); } return false; }
Kernel32.INSTANCE.CloseHandle(process);
Kernel32.INSTANCE.CloseHandle(phThreadToken.getValue()); phThreadToken.setValue(null);
} finally { if (phToken.getValue() != WinBase.INVALID_HANDLE_VALUE) { if (!Kernel32.INSTANCE.CloseHandle(phToken.getValue())) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError());