JNA – 查询Windows进程

我正在尝试使用JNA来返回特定Windows进程的详细信息。 不完全确定如何做到这一点。 无法在互联网上find很多帮助。 我想返回的一些信息包括CPU和内存使用情况。 下面只是我find的一个例子。

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import com.sun.jna.*; import com.sun.jna.Library.Handler; import com.sun.jna.platform.win32.*; import com.sun.jna.platform.win32.Advapi32Util.*; import com.sun.jna.platform.win32.WinNT.*; import com.sun.jna.ptr.IntByReference; import com.sun.jna.win32.*; import com.sun.jna.Native; import com.sun.jna.platform.win32.*; import com.sun.jna.win32.W32APIOptions; public class WindowsProcess { public static void main(String[] args) { WinNT winNT = (WinNT) Native.loadLibrary(WinNT.class, W32APIOptions.UNICODE_OPTIONS); WinNT.HANDLE snapshot = winNT.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0)); Thelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); while (winNT.Process32Next(snapshot, processEntry)) { System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile)); } winNT.CloseHandle(snapshot); } } 

这适用于JNA 3.5.0。 你想要的例子与更新版本的库不兼容,我想。

 import com.sun.jna.platform.win32.coreel32; 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) { coreel32 kernel32 = (coreel32) Native.loadLibrary(coreel32.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); } } } 

另请参阅我的答案。