-
Notifications
You must be signed in to change notification settings - Fork 431
/
ProcessHelper.cs
125 lines (115 loc) · 4.92 KB
/
ProcessHelper.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextHookLibrary
{
public static class ProcessHelper
{
const string ExtPath = "lib\\ProcessHelperExt.exe";
/// <summary>
/// 获得当前系统进程列表 形式:直接用于显示的字串和进程PID
/// </summary>
/// <returns></returns>
public static Dictionary<string, int> GetProcessList_Name_PID()
{
Dictionary<string, int> ret = new Dictionary<string, int>();
//获取系统进程列表
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowHandle != IntPtr.Zero)
{
string info = "";
info = p.ProcessName + "—" + p.Id;
ret.Add(info, p.Id);
}
p.Dispose();
}
return ret;
}
/// <summary>
/// 查找同名进程并返回一个进程PID列表
/// </summary>
/// <param name="pid"></param>
/// <returns></returns>
public static List<Process> FindSameNameProcess(int pid)
{
string DesProcessName = Process.GetProcessById(pid).ProcessName;
List<Process> res = new List<Process>();
foreach (Process p in Process.GetProcessesByName(DesProcessName))
res.Add(p);
return res;
}
/// <summary>
/// 根据进程PID找到程���所在路径
/// </summary>
/// <param name="pid"></param>
/// <returns></returns>
public static string FindProcessPath(int pid, bool isx64game = false)
{
try
{
Process p = Process.GetProcessById(pid);
return p.MainModule.FileName;
}
catch (System.ComponentModel.Win32Exception e)
{
if (!(isx64game && e.NativeErrorCode == 299 && System.IO.File.Exists(ExtPath)))
return "";
// Win32Exception:“A 32 bit processes cannot access modules of a 64 bit process.”
// 通过调用外部64位程序,使主程序在32位下获取其它64位程序的路径。外部程序不存在或不是此错误时保持原有逻辑返回""
var p = Process.Start(new ProcessStartInfo(ExtPath, pid.ToString())
{
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
});
string path = p.StandardOutput.ReadToEnd().TrimEnd();
if (p.ExitCode == 3) // 不存在此pid对应的进程
return "";
else if (p.ExitCode != 0)
throw new InvalidOperationException("Failed to execute ProcessHelper.exe");
return path;
}
}
/// <summary>
/// 返回 pid,绝对路径 的列表
/// </summary>
public static List<(int, string)> GetProcessesData(bool isx64game = false)
{
var l = new List<(int, string)>();
// 在32位主程序、64位游戏(或想获取全部进程)、存在外部程序时调用
if (isx64game && !Environment.Is64BitProcess && System.IO.File.Exists(ExtPath))
{
var p = Process.Start(new ProcessStartInfo(ExtPath)
{
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
});
string output = p.StandardOutput.ReadToEnd();
if (p.ExitCode != 0)
throw new InvalidOperationException("Failed to execute ProcessHelperExt.exe\n" + p.StandardError.ReadToEnd());
string[] lines = output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
var parts = line.Split('|');
l.Add((int.Parse(parts[0]), parts[1]));
}
}
else
foreach (var p in Process.GetProcesses())
using (p)
try { l.Add((p.Id, p.MainModule.FileName)); }
catch (System.ComponentModel.Win32Exception) { } // 无权限
catch (InvalidOperationException) { } // 进程已退出
return l;
}
/// <summary>
/// internal bool System.Diagnostics.ProcessManager.IsProcessRunning(int pid)
/// </summary>
public static Func<int, bool> IsProcessRunning = (Func<int, bool>)typeof(Process).Assembly.GetType("System.Diagnostics.ProcessManager").GetMethod("IsProcessRunning", new[] { typeof(int) }).CreateDelegate(typeof(Func<int, bool>));
}
}