Solved

Trapping crash in another process

Posted on 2006-10-30
20
238 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 20 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
 
LVL 44

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 44

Author Comment

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

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 30 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:DeepuAbrahamK
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 44

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 44

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 150 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 44

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 50 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 250 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 44

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 44

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 44

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 44

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now