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

The following examples show how to use com.sun.jna.platform.win32.Tlhelp32. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WinUtil.java    From SikuliX1 with MIT License 6 votes vote down vote up
public static List<ProcessInfo> allProcesses() {
  List<ProcessInfo> processList = new ArrayList<ProcessInfo>();

  HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(
      Tlhelp32.TH32CS_SNAPPROCESS, new DWORD(0));

  try {
    Tlhelp32.PROCESSENTRY32.ByReference pe
        = new Tlhelp32.PROCESSENTRY32.ByReference();
    for (boolean more = Kernel32.INSTANCE.Process32First(snapshot, pe);
         more;
         more = Kernel32.INSTANCE.Process32Next(snapshot, pe)) {
      int pid = pe.th32ProcessID.intValue();
      String name = getProcessImageName(pe.th32ProcessID.intValue());
      if (null == name) {
        continue;
      }
      processList.add(new ProcessInfo(pid, name));
    }
    return processList;
  } finally {
    Kernel32.INSTANCE.CloseHandle(snapshot);
  }
}
 
Example #2
Source File: Processes.java    From Java-Memory-Manipulation with Apache License 2.0 6 votes vote down vote up
public static Process byName(String name) {
	if (Platform.isWindows()) {
		Tlhelp32.PROCESSENTRY32.ByReference entry = new Tlhelp32.PROCESSENTRY32.ByReference();
		Pointer snapshot = Kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPALL.intValue(), 0);
		try {
			while (Kernel32.Process32NextW(snapshot, entry)) {
				String processName = Native.toString(entry.szExeFile);
				if (name.equals(processName)) {
					return byId(entry.th32ProcessID.intValue());
				}
			}
		} finally {
			Kernel32.CloseHandle(snapshot);
		}
	} else if (Platform.isMac() || Platform.isLinux()) {
		return byId(Utils.exec("bash", "-c", "ps -A | grep -m1 \"" + name + "\" | awk '{print $1}'"));
	} else {
		throw new UnsupportedOperationException("Unknown operating system! (" + System.getProperty("os.name") + ")");
	}
	throw new IllegalStateException("Process '" + name + "' was not found. Are you sure its running?");
}
 
Example #3
Source File: Processes.java    From Java-Memory-Manipulation with Apache License 2.0 6 votes vote down vote up
public static Process byName(String name) {
	if (Platform.isWindows()) {
		Tlhelp32.PROCESSENTRY32.ByReference entry = new Tlhelp32.PROCESSENTRY32.ByReference();
		Pointer snapshot = Kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPALL.intValue(), 0);
		try {
			while (Kernel32.Process32NextW(snapshot, entry)) {
				String processName = Native.toString(entry.szExeFile);
				if (name.equals(processName)) {
					return byId(entry.th32ProcessID.intValue());
				}
			}
		} finally {
			Kernel32.CloseHandle(snapshot);
		}
	} else if (Platform.isMac() || Platform.isLinux()) {
		return byId(Utils.exec("bash", "-c", "ps -A | grep -m1 \"" + name + "\" | awk '{print $1}'"));
	} else {
		throw new UnsupportedOperationException("Unknown operating system! (" + System.getProperty("os.name") + ")");
	}
	throw new IllegalStateException("Process '" + name + "' was not found. Are you sure its running?");
}
 
Example #4
Source File: CobaltStrike.java    From R9000 with Eclipse Public License 2.0 5 votes vote down vote up
static long findProcessID( String processName )
{
    Tlhelp32.PROCESSENTRY32.ByReference processInfo = new Tlhelp32.PROCESSENTRY32.ByReference();
    WinNT.HANDLE processSnapshotHandle =
                    kernel32.CreateToolhelp32Snapshot( Tlhelp32.TH32CS_SNAPPROCESS, new DWORD( 0L ) );

    try
    {
        kernel32.Process32First( processSnapshotHandle, processInfo );

        if ( processName.equals( Native.toString( processInfo.szExeFile ) ) )
        {
            return processInfo.th32ProcessID.longValue();
        }

        while ( kernel32.Process32Next( processSnapshotHandle, processInfo ) )
        {
            if ( processName.equals( Native.toString( processInfo.szExeFile ) ) )
            {
                return processInfo.th32ProcessID.longValue();
            }
        }

        return 0L;

    }
    finally
    {
        kernel32.CloseHandle( processSnapshotHandle );
    }
}
 
Example #5
Source File: Win32Process.java    From Java-Memory-Manipulation with Apache License 2.0 5 votes vote down vote up
@Override
public void initModules() {
    Pointer snapshot = Kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE32.intValue() | Tlhelp32.TH32CS_SNAPMODULE.intValue(), id());
    Tlhelp32.MODULEENTRY32W entry = new Tlhelp32.MODULEENTRY32W.ByReference();
    try {
        while (Kernel32.Module32NextW(snapshot, entry)) {
            String name = entry.szModule();
            modules.put(name, new Module(this, name, entry.hModule.getPointer(), entry.modBaseSize.intValue()));
        }
    } finally {
        Kernel32.CloseHandle(snapshot);
    }
}
 
Example #6
Source File: Win32Process.java    From Java-Memory-Manipulation with Apache License 2.0 5 votes vote down vote up
@Override
public void initModules() {
    Pointer snapshot = Kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE32.intValue() | Tlhelp32.TH32CS_SNAPMODULE.intValue(), id());
    Tlhelp32.MODULEENTRY32W entry = new Tlhelp32.MODULEENTRY32W.ByReference();
    try {
        while (Kernel32.Module32NextW(snapshot, entry)) {
            String name = entry.szModule();
            modules.put(name, new Module(this, name, entry.hModule.getPointer(), entry.modBaseSize.intValue()));
        }
    } finally {
        Kernel32.CloseHandle(snapshot);
    }
}
 
Example #7
Source File: SystemService.java    From winthing with Apache License 2.0 5 votes vote down vote up
@SuppressFBWarnings("DM_CONVERT_CASE")
public Map<Integer, String> findProcesses(final String nameFragment) {
    Objects.requireNonNull(nameFragment);

    final String lowercaseNameFragment = nameFragment.toLowerCase();
    final Map<Integer, String> processIds = new HashMap<>();

    final WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(
        Tlhelp32.TH32CS_SNAPPROCESS,
        null
    );
    try {
        final Tlhelp32.PROCESSENTRY32.ByReference entryReference =
            new Tlhelp32.PROCESSENTRY32.ByReference();
        if (kernel32.Process32First(snapshot, entryReference)) {
            while (kernel32.Process32Next(snapshot, entryReference)) {
                final String processName = new String(entryReference.szExeFile).trim();
                if (processName.toLowerCase().contains(lowercaseNameFragment)) {
                    processIds.put(entryReference.th32ProcessID.intValue(), processName);
                }
            }
        }
    } finally {
        kernel32.CloseHandle(snapshot);
    }

    return processIds;
}
 
Example #8
Source File: Kernel32.java    From Java-Memory-Manipulation with Apache License 2.0 votes vote down vote up
public static native boolean Process32NextW(Pointer pointer, Tlhelp32.PROCESSENTRY32 entry); 
Example #9
Source File: Kernel32.java    From Java-Memory-Manipulation with Apache License 2.0 votes vote down vote up
public static native boolean Module32NextW(Pointer pointer, Tlhelp32.MODULEENTRY32W entry); 
Example #10
Source File: Kernel32.java    From Java-Memory-Manipulation with Apache License 2.0 votes vote down vote up
public static native boolean Process32NextW(Pointer pointer, Tlhelp32.PROCESSENTRY32 entry); 
Example #11
Source File: Kernel32.java    From Java-Memory-Manipulation with Apache License 2.0 votes vote down vote up
public static native boolean Module32NextW(Pointer pointer, Tlhelp32.MODULEENTRY32W entry);