本文整理汇总了Java中com.sun.jna.platform.win32.WinNT.HANDLE属性的典型用法代码示例。如果您正苦于以下问题:Java WinNT.HANDLE属性的具体用法?Java WinNT.HANDLE怎么用?Java WinNT.HANDLE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类com.sun.jna.platform.win32.WinNT
的用法示例。
在下文中一共展示了WinNT.HANDLE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getWindowsProcessId
/**
* @param process NiFi Registry Process Reference
* @param logger Logger Reference for Debug
* @return Returns pid or null in-case pid could not be determined
* This method takes {@link Process} and {@link Logger} and returns
* the platform specific Handle for Win32 Systems, a.k.a <b>pid</b>
* In-case it fails to determine the pid, it will return Null.
* Purpose for the Logger is to log any interaction for debugging.
*/
private static Long getWindowsProcessId(final Process process, final Logger logger) {
/* determine the pid on windows plattforms */
try {
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handl = f.getLong(process);
Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE handle = new WinNT.HANDLE();
handle.setPointer(Pointer.createConstant(handl));
int ret = kernel.GetProcessId(handle);
logger.debug("Detected pid: {}", ret);
return Long.valueOf(ret);
} catch (final IllegalAccessException | NoSuchFieldException nsfe) {
logger.debug("Could not find PID for child process due to {}", nsfe);
}
return null;
}
示例2: while
/**
* 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 kernel32 = Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
boolean found = false;
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
String fname = Native.toString(processEntry.szExeFile);
if (fname != null && filenamePattern.matcher(fname).matches()) {
found = true;
break;
}
}
} finally {
kernel32.CloseHandle(snapshot);
}
return found;
}
示例3: if
/**
* Gets the handle of a process from the process entry.
*
* @param processEntry The processEntry to use
* @return The handle
* @throws AutomationException Thrown if the handle cannot be determined
*/
public static WinNT.HANDLE getHandleFromProcessEntry
(final Tlhelp32.PROCESSENTRY32.ByReference processEntry)
throws AutomationException {
ensureWinApiInstances();
WinNT.HANDLE handle = kernel32.OpenProcess (
0x0400 | /* PROCESS_QUERY_INFORMATION */
0x0800 | /* PROCESS_SUSPEND_RESUME */
0x0001 | /* PROCESS_TERMINATE */
0x00100000 /* SYNCHRONIZE */,
false,
processEntry.th32ProcessID.intValue());
if (handle == null) {
throw new AutomationException("OpenProcess failed");
}
return handle;
}
示例4: getProcessPid
/**
* Returns {@code pid} for Windows process
* @param process Windows process
* @return pid of the {@code process}
*/
public static int getProcessPid(Process process) {
if (process.getClass().getName().equals("java.lang.Win32Process") ||
process.getClass().getName().equals("java.lang.ProcessImpl")) {
try {
long handle = ReflectionUtil.getField(process.getClass(), process, long.class, "handle");
Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE winHandle = new WinNT.HANDLE();
winHandle.setPointer(Pointer.createConstant(handle));
return kernel.GetProcessId(winHandle);
} catch (Throwable e) {
throw new IllegalStateException(e);
}
} else {
throw new IllegalStateException("Unknown Process implementation");
}
}
示例5: isMassEffect3Running
/**
* Checks if MassEffect3.exe is currently running. Uses native code.
*
* @return
*/
public static boolean isMassEffect3Running() {
try {
Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
boolean result = false;
WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
if ("MassEffect3.exe".toUpperCase().equals(Native.toString(processEntry.szExeFile).toUpperCase())) {
result = true;
break;
}
}
} finally {
kernel32.CloseHandle(snapshot);
}
ModManager.debugLogger.writeMessage("Mass Effect 3 is " + (result ? "" : "not ") + "currently running.");
return result;
} catch (Throwable t) {
ModManager.debugLogger.writeErrorWithException("Critical native access exception: ", t);
ModManager.debugLogger.writeError("Mod Manager will report that the game is not running to continue normal operations.");
return false;
}
}
示例6: findProcessId
@Override
public String findProcessId(Process process) throws NoSuchFieldException, IllegalAccessException {
if (process.getClass().getName().equals("java.lang.Win32Process")
|| process.getClass().getName().equals("java.lang.ProcessImpl")) {
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handleNumber = f.getLong(process);
Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE handle = new WinNT.HANDLE();
handle.setPointer(Pointer.createConstant(handleNumber));
int pid = kernel.GetProcessId(handle);
log.debug("Found pid for managed process: {}", pid);
return pid + "";
}
return null;
}
示例7: getPageRanges
public static List<MEMORY_BASIC_INFORMATION> getPageRanges(WinNT.HANDLE hOtherProcess) {
List<MEMORY_BASIC_INFORMATION> ret = new ArrayList<>();
MEMORY_BASIC_INFORMATION mbi;
WinBase.SYSTEM_INFO si = new WinBase.SYSTEM_INFO();
Kernel32.INSTANCE.GetSystemInfo(si);
Pointer lpMem = si.lpMinimumApplicationAddress;
while (pointerToAddress(lpMem) < pointerToAddress(si.lpMaximumApplicationAddress)) {
mbi = new MEMORY_BASIC_INFORMATION();
BaseTSD.SIZE_T t = Kernel32.INSTANCE.VirtualQueryEx(hOtherProcess, lpMem, mbi, new BaseTSD.SIZE_T(mbi.size()));
if (t.longValue() == 0) {
Logger.getLogger(Win32ProcessTools.class.getName()).log(Level.SEVERE, "Cannot get page ranges. Last error:" + Kernel32.INSTANCE.GetLastError());
break;
}
ret.add(mbi);
lpMem = new Pointer(pointerToAddress(mbi.baseAddress) + mbi.regionSize.longValue());
}
return ret;
}
示例8: getChildren
private List<WinProcess> getChildren() throws IOException {
ArrayList<WinProcess> result = new ArrayList<WinProcess>();
WinNT.HANDLE hSnap = this.kernel32lib.CreateToolhelp32Snapshot(Kernel32Lib.TH32CS_SNAPPROCESS, new DWORD(0));
Kernel32Lib.PROCESSENTRY32.ByReference ent = new Kernel32Lib.PROCESSENTRY32.ByReference();
if (!this.kernel32lib.Process32First(hSnap, ent)) {
return result;
}
do {
if (ent.th32ParentProcessID.intValue() == this.pid) {
try {
result.add(new WinProcess(ent.th32ProcessID.intValue()));
}
catch (IOException e) {
System.err.println("WinProcess::getChildren, IOException " + e);
}
}
}
while (this.kernel32lib.Process32Next(hSnap, ent));
Kernel32.INSTANCE.CloseHandle(hSnap);
return result;
}
示例9: killWinProcess
private static int killWinProcess(Process process) {
int exitValue;
try {
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long hndl = f.getLong(process);
Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE handle = new WinNT.HANDLE();
handle.setPointer(Pointer.createConstant(hndl));
int pid = kernel.GetProcessId(handle);
killPID("" + pid);
exitValue = waitForProcessDeath(process, 10000);
} catch (Exception ex) {
LOG.log(Level.WARNING, "Process refused to die after 10 seconds, and couldn't taskkill it", ex);
throw new RuntimeException(
"Process refused to die after 10 seconds, and couldn't taskkill it: " + ex.getMessage(),
ex);
}
return exitValue;
}
示例10: windowsProcessId
@Nullable
private Long windowsProcessId(Object process) {
Class<?> clazz = process.getClass();
if (clazz.getName().equals("java.lang.Win32Process")
|| clazz.getName().equals("java.lang.ProcessImpl")) {
try {
Field f = clazz.getDeclaredField("handle");
f.setAccessible(true);
long peer = f.getLong(process);
Pointer pointer = Pointer.createConstant(peer);
WinNT.HANDLE handle = new WinNT.HANDLE(pointer);
return (long) Kernel32.INSTANCE.GetProcessId(handle);
} catch (Exception e) {
LOG.warn(e, "Cannot get process id!");
}
}
return null;
}
示例11: getWindowsPid
static long getWindowsPid(Process process) {
if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) {
try {
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handl = f.getLong(process);
Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE handle = new WinNT.HANDLE();
handle.setPointer(Pointer.createConstant(handl));
return kernel.GetProcessId(handle);
} catch (Throwable ignored) {
}
}
return -1;
}
示例12: findProcesses
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;
}
示例13: adjustPrivileges
public static boolean adjustPrivileges() {
WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(1);
WinNT.TOKEN_PRIVILEGES oldtp = new WinNT.TOKEN_PRIVILEGES(1);
WinNT.LUID luid = new WinNT.LUID();
WinNT.HANDLEByReference hTokenRef = new WinNT.HANDLEByReference();
if (!Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), WinNT.TOKEN_ADJUST_PRIVILEGES | WinNT.TOKEN_QUERY, hTokenRef)) {
return false;
}
WinNT.HANDLE hToken = hTokenRef.getValue();
if (!Advapi32.INSTANCE.LookupPrivilegeValue(null, WinNT.SE_DEBUG_NAME, luid)) {
Kernel32.INSTANCE.CloseHandle(hToken);
return false;
}
tp.PrivilegeCount = new WinDef.DWORD(1);
tp.Privileges = new WinNT.LUID_AND_ATTRIBUTES[1];
tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(luid, new WinDef.DWORD(WinNT.SE_PRIVILEGE_ENABLED));
IntByReference retSize = new IntByReference(0);
if (!Advapi32.INSTANCE.AdjustTokenPrivileges(hToken, false, tp, tp.size(), oldtp, retSize)) {
Kernel32.INSTANCE.CloseHandle(hToken);
return false;
}
Kernel32.INSTANCE.CloseHandle(hToken);
privAdjusted = true;
return true;
}
示例14: createEvent
public static WinNT.HANDLE createEvent() throws IOException {
WinNT.HANDLE hevent = kernel32.CreateEvent(null, false, false, null);
int res = kernel32.GetLastError();
if (hevent == WinBase.INVALID_HANDLE_VALUE || res!=0)
throw new IOException(JKernel32.getLastError());
return hevent;
}
示例15: findProcessPIDs
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;
}