🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #10762

Re: Detecting a process being killed

#10762
From: Bob Gonder
Date: Wed, 24 Mar 2004 15:46
55 lines
2885 bytes
Arnie Mauer wrote:

>I have a console app.  All of main runs in a try/catch block.  The app logs
>messages including successful completion or any exceptions caught.  The
>problem is that (a customer reported) it just disappears with no logging.
>I'm thinking someone might be using task manager to kill it.
>
>I built a test app and used task manager to kill it.  It does get killed.  I
>have set up signal handlers for:
>
>   signal( SIGINT, ImDead );
>
>but "ImDead" never gets called upon termination.  Is there any other
>mechanism that a process can use to detect if it's being killed?

I don't use exceptions, but use
		SetConsoleCtrlHandler( &CtrlHandler, TRUE);
And at least on XP, EndTask triggers it.
Note that CtrlHandler seems to run as a new thread (Your app is still
running, but will terminate soon after you return.)
I use CtrlHandler to close all open file handles, and to erase an
entry in a "who's running" file. 
Occasionally, the app throws an error for Invalid Handle because the
emergency shutdown has closed and invalidated the handles.


static BOOL _stdcall CtrlHandler(DWORD fdwCtrlType)
{
    switch (fdwCtrlType)
    { 
	/* CTRL+CLOSE: confirm that the user wants to exit. */
/*	Ctrl_Close _needs_ to return False because I don't want a
dialog poping up.*/
	case CTRL_CLOSE_EVENT:
		EmergencyShutdown();
		return FALSE;
 
        /* Pass other signals to the next handler. */ 
	case CTRL_LOGOFF_EVENT:
        case CTRL_SHUTDOWN_EVENT: 
	case CTRL_BREAK_EVENT:
		EmergencyShutdown();
		return TRUE;
	default:
	    return FALSE;
    }; 
}
static void EmergencyShutdown(void)
{
	remove_whoson();
	UnwindLockQue();
	UnwindFileQue();
	/*  remove myself from the handler queue  */
	SetConsoleCtrlHandler( &CtrlHandler, FALSE);
}
Technical Details
Message-ID: <dt9360tfhgb40mi73m5h19je95087hnc0q@4ax.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!border1.nntp.ash.giganews.com!border2.nntp.ash.giganews.com!nntp.giganews.com!in.100proofnews.com!in.100proofnews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4060d44c@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,4e1de9246721b866
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-03-24 08:07:12 PST
From: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Detecting a process being killed
Date: Wed, 24 Mar 2004 07:46:13 -0800
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <dt9360tfhgb40mi73m5h19je95087hnc0q@4ax.com>
References: <4060d44c@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 67.31.164.64
X-Trace: newsgroups.borland.com 1080143176 67.31.164.64 (24 Mar 2004 07:46:16 -0800)
Lines: 57
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!border1.nntp.ash.giganews.com!border2.nntp.ash.giganews.com!nntp.giganews.com!in.100proofnews.com!in.100proofnews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail