Article View: borland.public.cppbuilder.nativeapi
Article #1014Re: WM_QUIT and Hook
From: Remy Lebeau (TeamB)
Date: Wed, 31 Oct 2007 18:44
Date: Wed, 31 Oct 2007 18:44
70 lines
2842 bytes
2842 bytes
"Patryk B." <pbobak@no.spam.o2.pl> wrote in message
news:op.t0140zizwzezly@dune.intranet...
> Im traying to prevent to close my application.
> I know its function OnCloseQuery but i want to
> prevent closing from Task Manager too.
Since your own app also sends WM_QUIT to itself, there is no direct way to
know if WM_QUIT is coming from Task Manager or not. So you can't just block
WM_QUIT blindly, or else your app will never close at all. At the very
least, you will have to use the OnCloseQuery or OnClose event to set a flag
that your WM_QUIT handler can check for.
Try this instead (untested):
HHOOK hHook = NULL;
bool bIsQuitting = false;
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if( nCode == HC_ACTION )
{
PMSG msg = (PMSG) lParam;
if( msg->message == WM_QUIT )
{
MessageBox(NULL, "WM_QUIT detected!", "GetMsgProc", MB_OK);
if( !bIsQuitting )
msg->message = WM_NULL;
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
__fastcall TMainForm::TMainForm(TComponent *Owner)
: TForm(Owner)
{
hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC) &GetMsgProc,
NULL, MainThreadID);
if( !hHook )
ShowMessage("Couldnt set hook");
}
__fastcall TMainForm::~TMainForm()
{
if( hHook )
{
UnhookWindowsHookEx(hHook);
hHook = NULL;
}
}
void __fastcall TMainForm::FormCloseQuery(TObject *Sender, bool
&CanClose)
{
if( some condition )
CanClose = false;
}
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction
&Action)
{
bIsQuitting = true;
}
Gambit
Technical Details
Message-ID:
<4728cd60$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<op.t0140zizwzezly@dune.intranet>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,611b8a70eeab8356 X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <op.t0140zizwzezly@dune.intranet> Subject: Re: WM_QUIT and Hook Date: Wed, 31 Oct 2007 11:44:45 -0700 Lines: 72 Organization: Lebeau Software X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-RFC2646: Format=Flowed; Response NNTP-Posting-Host: 207.178.157.90 Message-ID: <4728cd60$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1193856352 207.178.157.90 (31 Oct 2007 11:45:52 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail