We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Trapping crash in another process

AndyAinscow
AndyAinscow asked
on
Medium Priority
301 Views
Last Modified: 2010-04-01
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");
  }
}
Comment
Watch Question

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.
Example:
if(CreateProcess(.....))
{
    if (WaitForMultipleObject(hProcess, INFINITE) == WAIT_FAILED)
    {
         int errorCode = GetLastError();
    }
  }
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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).
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Author

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.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Author

Commented:
I'll have a look at those links as well.
Deepu AbrahamR & D Engineering Manager
Commented:
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
Deepu AbrahamR & D Engineering Manager

Commented:
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
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Author

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. )

Commented:
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.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Author

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?
Commented:
Well, I expected something like Console application :)

MFC:
http://www.microsoft.com/msj/archive/S204D.aspx
See the second question.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Author

Commented:
That looks very interesting, it opens up other possibilities.  I'll have to think about what I really do require.
jkr
CERTIFIED EXPERT
Top Expert 2012
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.

>>>>    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
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Author

Commented:
Other things keep getting in the way - I'll try to get back to this next week.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Author

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

Commented:
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 :)
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Author

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.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.