Java Examples for com.sun.jna.platform.win32.Tlhelp32

The following java examples will help you to understand the usage of com.sun.jna.platform.win32.Tlhelp32. These source code samples are taken from different open source projects.

Example 1
Project: autopsy-master  File: Win32Process.java View source code
/**
     * Get children of current process object.
     *
     * @return list of child processes
     *
     * @throws IOException
     */
public List<Win32Process> getChildren() throws Exception {
    ArrayList<Win32Process> result = new ArrayList<>();
    WinNT.HANDLE hSnap = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new DWORD(0));
    Tlhelp32.PROCESSENTRY32.ByReference ent = new Tlhelp32.PROCESSENTRY32.ByReference();
    if (!Kernel32.INSTANCE.Process32First(hSnap, ent)) {
        return result;
    }
    do {
        if (ent.th32ParentProcessID.intValue() == pid) {
            result.add(new Win32Process(ent.th32ProcessID.intValue()));
        }
    } while (Kernel32.INSTANCE.Process32Next(hSnap, ent));
    Kernel32.INSTANCE.CloseHandle(hSnap);
    return result;
}
Example 2
Project: jna-master  File: AbstractWin32TestSupport.java View source code
public static void killProcessByName(String filename) {
    HANDLE hSnapShot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(TH32CS_SNAPALL, null);
    Tlhelp32.PROCESSENTRY32 process = new Tlhelp32.PROCESSENTRY32();
    boolean hRes = Kernel32.INSTANCE.Process32First(hSnapShot, process);
    while (hRes) {
        String imageName = Native.toString(process.szExeFile);
        if (imageName.equalsIgnoreCase(filename)) {
            HANDLE hProcess = Kernel32.INSTANCE.OpenProcess(Kernel32.PROCESS_TERMINATE, false, process.th32ProcessID.intValue());
            if (hProcess != null) {
                Kernel32.INSTANCE.TerminateProcess(hProcess, 9);
                Kernel32.INSTANCE.CloseHandle(hProcess);
            }
        }
        hRes = Kernel32.INSTANCE.Process32Next(hSnapShot, process);
    }
    Kernel32.INSTANCE.CloseHandle(hSnapShot);
}
Example 3
Project: massif-master  File: MatlabRunningManager.java View source code
private static Map<String, Integer> findProcessPIDs(Kernel32 kernel32) {
    Map<String, Integer> processes = new HashMap<String, Integer>();
    String matlabExe = "matlab.exe";
    Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
    // gets all current running processes
    WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
    if (kernel32.Process32First(snapshot, processEntry)) {
        while (kernel32.Process32Next(snapshot, processEntry)) {
            String exePath = Native.toString(processEntry.szExeFile);
            exePath = exePath.toLowerCase();
            // check if its a matlab process
            if (!exePath.equalsIgnoreCase(matlabExe)) {
                continue;
            }
            WinNT.HANDLE hProcess = kernel32.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processEntry.th32ProcessID.intValue());
            // gets process path
            if (hProcess != null && hProcess.getPointer() != null) {
                char[] filePath = new char[1024];
                Psapi32.INSTANCE.GetModuleFileNameExW(hProcess.getPointer(), null, filePath, 256);
                String processPath = Native.toString(filePath);
                int pid = kernel32.GetProcessId(hProcess);
                processes.put(processPath, pid);
            }
        }
    }
    return processes;
}