java获取当前系统所有正在运行的进程
2024-11-04 15:21:41 # 文章收藏

获取当前系统所有正在运行的进程

ProcList类

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{

//data members
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[]>();;

//getting proc details (without path)
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 {

//looping ProcList
while (kernel32.Process32Next(processSnapshot, processEntry)){

//assigning details
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);

//final 2d result array
procData.add(new String[] {dwSize, cntUsage, th32ProcessID, th32DefaultHeapID, th32ModuleID, cntThreads, th32ParentProcessID, pcPriClassBase, pcPriClassBase, dwFlags, szExeFile});

}
}
finally {
kernel32.CloseHandle(processSnapshot);
}

//returning data
return procData;

}

//getting proc details (with path)
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 {

//looping ProcList
while (kernel32.Process32Next(processSnapshot, processEntry)){

//assigning details
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);

//getting path
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();


//final 2d result array
procData.add(new String[] {dwSize, cntUsage, th32ProcessID, th32DefaultHeapID, th32ModuleID, cntThreads, th32ParentProcessID, pcPriClassBase, pcPriClassBase, dwFlags, szExeFile, procPath});


}
}
finally {
kernel32.CloseHandle(processSnapshot);
}

//returning data
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++) {
//System.out.println(Arrays.toString(test.get(i)));
//System.out.println(test.get(i)[2]+"---"+test.get(i)[10]);
// coursesMap.put("PID",test.get(i)[2]);
// coursesMap.put("processName",test.get(i)[10]);

// System.out.println("PID" + ":" + test.get(i)[2]);
// System.out.println("processName" + ":" + test.get(i)[10]);
sjson=sjson+"{"+"\"PID\"" + ":" +"\""+ test.get(i)[2] +"\""+","+"\"processName\"" + ":" +"\""+ test.get(i)[10]+"\""+"},";
}
sjson ="["+sjson.substring(0,sjson.length() - 1)+"]";
return sjson;
// for (int i = 0; i < test.size(); i++) {
// //System.out.println(Arrays.toString(test.get(i)));
// //System.out.println(test.get(i)[2]+"---"+test.get(i)[10]);
// coursesMap.put(test.get(i)[2], test.get(i)[10]);
// }
// return coursesMap;
}


public static void main(String[] args) {
List<String[]> test = new ArrayList<String[]>();
ProcList pl = new ProcList();
test = pl.getProcListExt();
// for (String[] row : test) {
// System.out.println(row[0]+"\t"+row[9]+"\t\t"+row[10]);
// }
// for (String[] strings : test) {
// System.out.println("pid: "+strings[2]+"---"+"name: "+strings[10]);
// }
Map<String, String> coursesMap =new HashMap<String, String>();
// for (String[] strings : test) {
// System.out.println("pid: "+strings[2]+"---"+"name: "+strings[10]);
// }
System.out.println(test.size());
String sjson="";
for (int i = 0; i < test.size(); i++) {
//System.out.println(Arrays.toString(test.get(i)));
//System.out.println(test.get(i)[2]+"---"+test.get(i)[10]);
// coursesMap.put("PID",test.get(i)[2]);
// coursesMap.put("processName",test.get(i)[10]);

// System.out.println("PID" + ":" + test.get(i)[2]);
// System.out.println("processName" + ":" + test.get(i)[10]);
sjson=sjson+"{"+"\"PID\"" + ":" + "\"test.get(i)[2]\""+","+"\"processName\"" + ":" + "\"test.get(i)[10]\""+"},";

}
sjson =sjson.substring(0,sjson.length() - 1);
System.out.println(sjson);
// coursesMap.entrySet().stream().forEach((entry) -> {
// System.out.println(entry.getKey()+"---"+entry.getValue());
// });
}
}

ProcessPathKernel32接口

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
package com.wonder.utils;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.win32.W32APIOptions;

public interface ProcessPathKernel32 extends Kernel32 {
class MODULEENTRY32 extends Structure {
public static class ByReference extends MODULEENTRY32 implements Structure.ByReference {
public ByReference() {
}

public ByReference(Pointer memory) {
super(memory);
}
}
public MODULEENTRY32() {
dwSize = new WinDef.DWORD(size());
}

public MODULEENTRY32(Pointer memory) {
super(memory);
read();
}


public DWORD dwSize;
public DWORD th32ModuleID;
public DWORD th32ProcessID;
public DWORD GlblcntUsage;
public DWORD ProccntUsage;
public Pointer modBaseAddr;
public DWORD modBaseSize;
public HMODULE hModule;
public char[] szModule = new char[255+1]; // MAX_MODULE_NAME32
public char[] szExePath = new char[MAX_PATH];
public String szModule() { return Native.toString(this.szModule); }
public String szExePath() { return Native.toString(this.szExePath); }
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] {
"dwSize", "th32ModuleID", "th32ProcessID", "GlblcntUsage", "ProccntUsage", "modBaseAddr", "modBaseSize", "hModule", "szModule", "szExePath"
});
}
}

ProcessPathKernel32 INSTANCE = (ProcessPathKernel32)Native.loadLibrary(ProcessPathKernel32.class, W32APIOptions.UNICODE_OPTIONS);
boolean Module32First(HANDLE hSnapshot, MODULEENTRY32.ByReference lpme);
boolean Module32Next(HANDLE hSnapshot, MODULEENTRY32.ByReference lpme);
}