I need to write a tool, that listens for some target process(which crashes in customer environment) and as soon as it crashes it should generate a dump by launching DebugDiag passing it the command line parameters. The code needs to be written in C#. I have already done some coding but the tool never detects the process launched. Here is the code:
static void Main(string[] args)
{
ManagementEventWatcher startWatch = new ManagementEventWatcher(
new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
startWatch.EventArrived
+= new EventArrivedEventHandler(startWatch_EventArrived);
startWatch.Start();
Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
startWatch.Stop();
}
static void startWatch_EventArrived(object sender, EventArrivedEventArgs e)
{
string name = e.NewEvent.Properties["ProcessName"].Value as string;
Console.WriteLine("Process started: {0}", name);
if (name != null && name.Contains("My Process.exe"))
{
string procpath = "C:\\Program Files\\DebugDiag";
string filename = Path.Combine(procpath, "DbgHost.exe");
var proc = System.Diagnostics.Process.Start(filename, "-dump My Process.exe");
}
}
Please also advise if this is the way to pass command line parameters to DebugDiag @Bruno, i implemented your suggestion using ProcDump. Now it works once, which means when i launch my target process(32 bit) ProcDump also launches, however my application is such that when i launch a workspace within, it launches another process with the same name and this time ProcDump failed to lauch, upon debugging i found that it threw an exception saying 32-bit process cannot debug 64-bit process and all my target processes are 32-bit only... Code:
static void startWatch_EventArrived(object sender, EventArrivedEventArgs e)
{
string name = e.NewEvent.Properties["ProcessName"].Value as string;
Console.WriteLine("Process started: {0}", name);
if (name != null && name.Contains("MyProcess.exe"))
{
string procpath = "C:\\Procdump";
string filename = Path.Combine(procpath, "procdump.exe");
var proc = System.Diagnostics.Process.Start(filename, "-e -f -mp -n 25 -w -accepteula MyProcess.exe MyProcess_crash");
}
}