?
Solved

Trapping crash in another process

Posted on 2006-10-30
20
Medium Priority
?
282 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");
  }
}
0
Comment
Question by:AndyAinscow
  • 9
  • 4
  • 3
  • +3
20 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17832585
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
 
LVL 12

Assisted Solution

by:rajeev_devin
rajeev_devin earned 80 total points
ID: 17832596
Example:
if(CreateProcess(.....))
{
    if (WaitForMultipleObject(hProcess, INFINITE) == WAIT_FAILED)
    {
         int errorCode = GetLastError();
    }
  }
}
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17832603
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 45

Author Comment

by:AndyAinscow
ID: 17832640
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17832659
0
 
LVL 45

Author Comment

by:AndyAinscow
ID: 17832688
I'll have a look at those links as well.
0
 
LVL 11

Assisted Solution

by:Deepu Abraham
Deepu Abraham earned 120 total points
ID: 17833104
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
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 17833138
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
 
LVL 45

Author Comment

by:AndyAinscow
ID: 17833160
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17833205
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
 
LVL 45

Author Comment

by:AndyAinscow
ID: 17833233
<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
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 600 total points
ID: 17833477
Well, I expected something like Console application :)

MFC:
http://www.microsoft.com/msj/archive/S204D.aspx
See the second question.
0
 
LVL 45

Author Comment

by:AndyAinscow
ID: 17833605
That looks very interesting, it opens up other possibilities.  I'll have to think about what I really do require.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 17834560
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 1000 total points
ID: 17835246

>>>>    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
 
LVL 45

Author Comment

by:AndyAinscow
ID: 17860740
Other things keep getting in the way - I'll try to get back to this next week.
0
 
LVL 45

Author Comment

by:AndyAinscow
ID: 17905632
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17905651
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
 
LVL 45

Author Comment

by:AndyAinscow
ID: 17905744
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
 
LVL 45

Author Comment

by:AndyAinscow
ID: 17905826
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question