views:

61

answers:

2

In Visual C++ when terminate() is called the default behavior is to call abort() which by default shows a message box and then - after OK button on the message box is pressed - terminates the application. The "shows message box" part is not very good for programs that must work without human interaction since the program just hangs until the button is pressed.

In VC++8 Microsoft introduced _set_abort_behavior() function that can be called at application startup and prohibit showing the message box in abort().

How do I achieve the same in VC++7 and earlier? I could write my custom terminate() handler, but what is the best action to invoke inside it so that the program terminates the same way as with abort() but without the message box?

+2  A: 

Call the operating system's process terminate function. TerminateProcess() on Windows.

Hans Passant
+1 - nobugz. This is a good one if were happy for a process to die instantly. Not recommended if you want it to die in a tidy manner though.
ChrisBD
A: 

WM_CLOSE message?

ChrisBD
Who will process the message if I'm inside the terminate handler that is not supposed to return to the program?
sharptooth
I've made the assumption that you were aiming for Windows OS. If you have the process handle you just postmessage the WM_CLOSE to the application that you want to shutdown.Otherwise nobugz answer is a good one.
ChrisBD
In order to shutdown this way (with posting a message) I'll need to return from the terminate() handler and this is not allowed.
sharptooth
Well PostMessage is an asynchronous operation and its handled by the message pump within the destination application, but I've probably misunderstood something here.What type of process are you trying to terminate here? Is it a subprocess of your current application? Your current application? Or an entirely separate application process? Is it even on the same machine or operating system?
ChrisBD
@ChrisBD: No, that's the same process. terminate() is called on so-called double exception and the program is expected to do something simple and then exit, so the terminate() handler is not allowed to return control, so noone will be able to handle that message.
sharptooth
Odd. It was a common enough trick with VC++ 6.0 and I don't recall any message boxes popping up. Is this a problem linked to Vista perhaps as the UIPI will cause problems I would imagine.
ChrisBD
Ah ... wait the light dawns you're looking at using this within your custom terminate handler of the terminating process, that was not my intention. I thought that you were looking for a way of closing a process from another.What type of VC++ are you using? MFC? Moderated?
ChrisBD
@ChrisBD: Plain old Visual C++ 7 without MFC.
sharptooth