如何从 werfault 进程中获取冻结的进程 ID [英] How to get frozen process id from werfault process
问题描述
我想知道这是否可以从 C# 中的 werfault 进程中获取相关进程 ID.我不想禁用 werfault 服务,只想获取相关(冻结)进程 ID.我写了这段代码:
I'm wondering is this possible to get related process id from werfault process in C#. I don't want to disable werfault service, only get a related (frozen) process id. I wrote this code:
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.ProcessName.ToLower().Contains("werfault"))
{
//getting related process id?
}
}
例如:werfault 服务报告programX 已停止工作".我正在使用上面的代码来查找 werfault 进程,然后杀死它并检索 programX pid(我现在不能这样做).
For example: werfault service reporting that 'programX has stopped working'. I'm using the code above to find werfault process and then kill it and retrieving programX pid (which I can't do right now).
我在这里找到了部分答案:如何启动崩溃(很少)子进程中的应用程序,但这适用于python.
I found partial answer here: How to launch crashing (rarely) application in subprocess but this works for python.
这可以取回吗?我需要任何外部库吗?
Is this possible to retrieve? Do I need any external libraries?
推荐答案
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.ProcessName.ToLower().Contains("werfault"))
{
// Get the CommandLine string from the werfault.exe
string startupParam = GetCommandLine(p);
// Get the ProcessID of the frozen Process.
// Sure you can optimize this part, but it works in this case :)
int pID = int.Parse(startupParam.Split(new string[] { "-p" }, StringSplitOptions.None).
Last().Split(new string[] { "-s" }, StringSplitOptions.None).First().Trim());
// Get the frozen Process.
Process frozenProcess = Process.GetProcessById(pID);
}
}
/// <summary>
/// Returns the CommandLine from a Process.
/// </summary>
/// <param name="process"></param>
/// <returns></returns>
private static string GetCommandLine(Process pProcess)
{
// Create a new CommandLine with the FileName of the given Process.
var commandLine = new StringBuilder(pProcess.MainModule.FileName);
commandLine.Append(" ");
// Now we need to query the CommandLine of the process with ManagementObjectSearcher.
using (var searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + pProcess.Id))
{
// Append the arguments to the CommandLine.
foreach (var @object in searcher.Get())
{
commandLine.Append(@object["CommandLine"]);
commandLine.Append(" ");
}
}
// Return the CommandLine.
return commandLine.ToString();
}
这篇关于如何从 werfault 进程中获取冻结的进程 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!