2

Possible Duplicate:
Detecting process crash in .NET

I'm writing a c# program that have to determine if another C++ game program (let's call it Foobar) is crashed or not. When the FooBar program crashes it's notifying the user about the crash with a MessageBox, if you OK that windows the program closes. So I guess I could determine if the program is crashed if that messagebox is opened/active. Problem I dont know how to do that. Or if there is any other better solution comes to your mind, please share it with me.

Edit: I can not edit the C++ program, and it's always a possibility that it will crash. I just need to know if it did.

Community
  • 1
  • 1
Little Monroe
  • 23
  • 1
  • 4
  • I think your real problem is "why does Foobar crash", not "how can I detect the crash" – L.B Aug 28 '12 at 21:17
  • Can you modify the C++ program? Is there a distinction that you need to process between "crashed" and "closed"? What if you just monitor the processes list? If that process disappears then it's either crashed or been closed. – itsme86 Aug 28 '12 at 21:18
  • Assuming you actually mean "crash", you have several issues to address: 1) How will you be notified of a crash? 2) What do you want to know about the crash? 3) What will you do in response to the crash? Here's one possible suggestion for "1)": http://support.microsoft.com/kb/310490 – paulsm4 Aug 28 '12 at 21:19
  • http://stackoverflow.com/questions/2309836/detecting-process-crash-in-net – mrranstrom Aug 28 '12 at 21:27
  • If you're solely looking for the message box, try `FindWindow`. I agree there are many better ways than that, though. – chris Aug 28 '12 at 21:36
  • @chris So you know any more ways, or just agreeing that there must be other ways? :) If you know more please share! – Little Monroe Aug 29 '12 at 14:40
  • @L.B No, first of all I dont have the source, and every program can and will crash eventually, I'm just preparing my self :) – Little Monroe Aug 29 '12 at 15:33

2 Answers2

3

Duplicate of Detecting process crash in .NET.

The heartbeat is probably the way to go, but there is another way.

When a process crashes, Windows first checks to see if a Just-In-Time debugger is configured on your system. If so, you can have that debugger attach itself to the process right before it crashes. Usually you would use this functionality to generate a memory dump as a process crashes. Whatever debugger you attach gets to know the PID and name of the crashing process. You can either utilize features of existing debugging tools, such as ADPlus, or write your own program and tell Windows that that is your Just-In-Time debugger and should be run when a process crashes. I believe you can set up a JIT debugger specifically for any process name.

See http://msdn.microsoft.com/en-us/library/5hs4b7a6(v=VS.80).aspx

I think that if you set the HKLM\Software\Microsoft\Windows NT\Current Version\AeDebug\Debugger registry entry to '"DirectoryOfYourProgram\YourProgram.exe" -p %ld' where YourProgram.exe expects a PID passed in with the -p flag, your program will be called and passed the correct PID when a process crashes.

Community
  • 1
  • 1
mrranstrom
  • 228
  • 2
  • 10
  • If it's a duplicate, you should put your answer on that question, not this one, since this one should be closed. – Rob Kennedy Aug 29 '12 at 14:54
  • I have to know if the program crashed runtime, so the starting my program with parameter is not a sln :( And by the way my app is .NET the appliaction I need to watch is C++ – Little Monroe Aug 29 '12 at 15:08
  • Let's call your .NET app Baz. FooBar and YourProgram.exe are as above. You can write an intermediary program to let Baz know that FooBar crashed. You can program YourProgram.exe to simply send a message off to Baz when FooBar crashes. You'll just have to program Baz to listen for the message. Baz can always be running, but be notified when FooBar crashes via YourProgram.exe. – mrranstrom Aug 29 '12 at 16:59
  • No problem. This is exactly what I'm working on right now. – mrranstrom Aug 29 '12 at 19:47
1

If you have access to the code for both apps, you could setup a heartbeat message from the c++ app to the c# app, and do whatever when the heartbeats stop. For the comms you would need something like named pipes or similar. Alternatively your c++ could write to a file regularly, which your c# could detect updates to, using file monitoring.

Surfbutler
  • 1,529
  • 2
  • 17
  • 38
  • even if an application is crashed, some threads may continue to work giving false heartbeats. Try this `public void RunMe() { Task.Factory.StartNew(() => { while (true) { Thread.Sleep(1000); Console.WriteLine(DateTime.Now.ToString()); } } ); object x = null; x.ToString(); }` – L.B Aug 28 '12 at 21:33
  • @L.B That is an issue many heartbeat implementations need to overcome. The mechanism that sends the heartbeat will also need to check whether or not it should send. – Kaiged Aug 28 '12 at 21:35
  • This is definitely a good idea, even though I dont have the source for the c++ app. – Little Monroe Aug 29 '12 at 14:41
  • 1
    Without the source, you should probably try mrranstrom's solution above, and attach a JIT debugger. – Surfbutler Aug 29 '12 at 15:06
  • @Surfbutler Can I attach a JIT debugger to a C++ program? – Little Monroe Aug 29 '12 at 15:29
  • Yes, any executable, including one you didn't write yourself. – Surfbutler Aug 29 '12 at 15:58