Trapping crash in another process

How would I go about starting another application and catching an unhandled exception / crash.  

eg.

if(CreateProcess(.....))
{
  try
  {
    while(processRunning)
    {
      //idle away - everything OK
    }
    cleanup and return
  }
  catch(ErrorInProcess)
  {
    msgbox("oh dear - it has crashed");
  }
}
LVL 45
AndyAinscowFreelance programmer / ConsultantAsked:
Who is Participating?
 
itsmeandnobodyelseConnect With a Mentor Commented:

>>>>    if (WaitForMultipleObject(hProcess, INFINITE) == WAIT_FAILED)

there is no WAIT_FAILED return if the app has crashed. The handle gets signalled and the return value is WAIT_OBJECT_0 as it would be for a successful return.

To retrieve additional information you may call GetExitCodeProcess which should return 0 if the app terminated successfully or different to 0 otherwise. Note, if the app catches the exception and calls abort() - what isn't very graceful - you'll get exit code 3. If the app calls exit(n) you'll get n as exit code. In a MFC app you can override CWinApp::ExitInstance to return an exit code different from 0. If you handle exceptions as recommended in the article of Paul DiLascia you might want to return an exit code different from 0 if the user decides to abort after save. You could do that by setting an appropriate member in CMyApp which was used in ExitInstance afterwards.

Regards, Alex
0
 
rajeev_devinCommented:
Are you using WaitForSingleObject(...) for waiting.
If yes then the return value of this function tells you about the status of the process.
Whether it terminated successfully, or some error creep in.
0
 
rajeev_devinConnect With a Mentor Commented:
Example:
if(CreateProcess(.....))
{
    if (WaitForMultipleObject(hProcess, INFINITE) == WAIT_FAILED)
    {
         int errorCode = GetLastError();
    }
  }
}
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
rajeev_devinCommented:
Check this link for details

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitforsingleobject.asp

Finally, I hope you are using window (As your CreateProcess function tells that you are).
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
Yes, it is a window based app ('normal' exe with a window that is).  I'll give it a try later and get back.
0
 
AlexFMCommented:
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
I'll have a look at those links as well.
0
 
Deepu AbrahamConnect With a Mentor R & D Engineering ManagerCommented:
The GetLastError function retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code.So I don't think it will show the exact error happened inside the child process.

IMHO, if you have control over your child application then you need to use an IPC to communicate it propper to other application/parent application.Hence you will have more control on your application.

Best Regards,
DeepuAbrahamK
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
Child process code should have:

try
{
...
//Code
...
}
catch(...)
{
      GetLastError()
      //Do some IPC calls to Parent Application
      Exit Here...
}

Hope this makes my idea clear to you.
Best Regards,
DeepuAbrahamK
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
IPC - Good point.  
At present this functionality may not even be required, should it be required it is initially just to trap the case of another app crashing, the why is a little less important at present.  ( Often it would be nice to have PQR and XYZ but someone ultimately has to pay for it and wait for it to be coded. )
0
 
AlexFMCommented:
If we can change code of child application, this is simple - just catch exception in the child, and there are 1000 ways to pass this information to parent. Interesting case is to do this for any child application, this depends on your requirements.
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
<If we can change code of child application, this is simple - just catch exception in the child,>

You may have some knowledge that I don't.  eg. MFC - SDI based app, where do I put ONE try..catch block to trap some exception anywhere in the app?
0
 
AlexFMConnect With a Mentor Commented:
Well, I expected something like Console application :)

MFC:
http://www.microsoft.com/msj/archive/S204D.aspx
See the second question.
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
That looks very interesting, it opens up other possibilities.  I'll have to think about what I really do require.
0
 
jkrConnect With a Mentor Commented:
If you are really serious about *detecting* these exceptions, you indeed will have to write a 'mini' debugger. See also http://msdn2.microsoft.com/en-us/library/ms809754.aspx ("The Win32 Debugging Application Programming Interface"). Also, John Robbins' "Bugslayer" columns (e.g. http://www.microsoft.com/msj/0299/bugslayer/bugslayer0299.aspx) might add some additional insight.
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
Other things keep getting in the way - I'll try to get back to this next week.
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
AlexFM, I've created another question specifically to award you points related to trapping an exception internally in an app.  Not what I asked for here but a very useful alternative.
Please make a comment there.

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_22054603.html
0
 
AlexFMCommented:
Andy, thank you, but "Points for" is not allowed now in EE. It's enough for me to know that my answer is OK. You can remove another question before some spammer didn't post some stupid link there :)
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
It was an alternative that for me didn't match what I asked for here (hence I would have argued that it didn't go over the 500pts per question, because it was really a separate question).  
I'll remove the other question and thank you for the information.
0
 
AndyAinscowFreelance programmer / ConsultantAuthor Commented:
Awk - forget to hit submit before splitting points.

Thanks guys.  At present I just want something simple so I haven't investigated the mini-debugger.  All I have as a requirement at present is to see if the app being launched exits gracefully or not.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.