1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| package com.wonder.utils; import java.util.*; import com.sun.jna.Native; 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;
public class ProcList{
private String dwSize; private String cntUsage; private String th32ProcessID; private String th32DefaultHeapID; private String th32ModuleID; private String cntThreads; private String th32ParentProcessID; private String pcPriClassBase; private String dwFlags; private String szExeFile; private String procPath; private List<String[]> procData = new ArrayList<String[]>();;
public List<String[]> getProcList(){
Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.DEFAULT_OPTIONS); Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); WinNT.HANDLE processSnapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
try {
while (kernel32.Process32Next(processSnapshot, processEntry)){
dwSize = processEntry.dwSize.toString(); cntUsage = processEntry.cntUsage.toString(); th32ProcessID = processEntry.th32ProcessID.toString(); th32DefaultHeapID = processEntry.th32DefaultHeapID.toString(); th32ModuleID = processEntry.th32ModuleID.toString(); cntThreads = processEntry.cntThreads.toString(); th32ParentProcessID = processEntry.th32ParentProcessID.toString(); pcPriClassBase = processEntry.pcPriClassBase.toString(); dwFlags = processEntry.dwFlags.toString(); szExeFile = Native.toString(processEntry.szExeFile);
procData.add(new String[] {dwSize, cntUsage, th32ProcessID, th32DefaultHeapID, th32ModuleID, cntThreads, th32ParentProcessID, pcPriClassBase, pcPriClassBase, dwFlags, szExeFile});
} } finally { kernel32.CloseHandle(processSnapshot); }
return procData;
}
public List<String[]> getProcListExt(){
Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.DEFAULT_OPTIONS); Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); WinNT.HANDLE processSnapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
try {
while (kernel32.Process32Next(processSnapshot, processEntry)){
dwSize = processEntry.dwSize.toString(); cntUsage = processEntry.cntUsage.toString(); th32ProcessID = processEntry.th32ProcessID.toString(); th32DefaultHeapID = processEntry.th32DefaultHeapID.toString(); th32ModuleID = processEntry.th32ModuleID.toString(); cntThreads = processEntry.cntThreads.toString(); th32ParentProcessID = processEntry.th32ParentProcessID.toString(); pcPriClassBase = processEntry.pcPriClassBase.toString(); dwFlags = processEntry.dwFlags.toString(); szExeFile = Native.toString(processEntry.szExeFile);
WinNT.HANDLE moduleSnapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, processEntry.th32ProcessID); ProcessPathKernel32.MODULEENTRY32.ByReference me = new ProcessPathKernel32.MODULEENTRY32.ByReference(); ProcessPathKernel32.INSTANCE.Module32First(moduleSnapshot, me); procPath = me.szExePath();
procData.add(new String[] {dwSize, cntUsage, th32ProcessID, th32DefaultHeapID, th32ModuleID, cntThreads, th32ParentProcessID, pcPriClassBase, pcPriClassBase, dwFlags, szExeFile, procPath});
} } finally { kernel32.CloseHandle(processSnapshot); }
return procData;
}
public String PidAndName() { List<String[]> test = new ArrayList<String[]>(); Map<String, String> coursesMap =new HashMap<String, String>(); ProcList pl = new ProcList(); test = pl.getProcListExt(); String sjson=""; for (int i = 0; i < test.size(); i++) {
sjson=sjson+"{"+"\"PID\"" + ":" +"\""+ test.get(i)[2] +"\""+","+"\"processName\"" + ":" +"\""+ test.get(i)[10]+"\""+"},"; } sjson ="["+sjson.substring(0,sjson.length() - 1)+"]"; return sjson;
}
public static void main(String[] args) { List<String[]> test = new ArrayList<String[]>(); ProcList pl = new ProcList(); test = pl.getProcListExt();
Map<String, String> coursesMap =new HashMap<String, String>();
System.out.println(test.size()); String sjson=""; for (int i = 0; i < test.size(); i++) {
sjson=sjson+"{"+"\"PID\"" + ":" + "\"test.get(i)[2]\""+","+"\"processName\"" + ":" + "\"test.get(i)[10]\""+"},";
} sjson =sjson.substring(0,sjson.length() - 1); System.out.println(sjson);
} }
|