Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages
Started by Patryk B.
Wed, 31 Oct 2007 10:35
WM_QUIT and Hook
Author: Patryk B.
Date: Wed, 31 Oct 2007 10:35
Date: Wed, 31 Oct 2007 10:35
53 lines
1965 bytes
1965 bytes
Im traying to prevent to close my application.
I know its function OnCloseQuery but i want to prevent closing
from Task Manager too.
In know that global hook should be placed in DLL.
But I want hook only for my application.
What is wrong? It dont detect WM_QUIT.
Form6.cpp:
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode < 0)
{
return CallNextHookEx(hWin, nCode, wParam, lParam);
}
if(nCode==HC_ACTION)
{
PMSG msg;
msg = (PMSG)lParam;
if(msg->message==WM_QUIT)
{
ShowMessage("WM_QUIT detected!");
}
}
return CallNextHookEx(hWin, nCode, wParam, lParam);
}
void __fastcall TForm6::Button1Click(TObject *Sender)
{
hWin= SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc, NULL,
GetWindowThreadProcessId(this->Handle, NULL));
if(hWin==NULL)
{
ShowMessage("Couldnt set hook");
}
}
void __fastcall TForm6::Button2Click(TObject *Sender)
{
if(hWin)
{
UnhookWindowsHookEx(hWin);
hWin=NULL;
}
}
Patryk B.
Re: WM_QUIT and Hook
Author: ohmy
Date: Wed, 31 Oct 2007 11:10
Date: Wed, 31 Oct 2007 11:10
4 lines
1037 bytes
1037 bytes
Windows will just kill your app, you can't prevent that. Nor should you. The way to go is perhaps to make your app invisible in the task manager; can't manually close what you can't see. Couldn't tell you how to do this though but I'm sure google can!
Re: WM_QUIT and Hook
Author: Bob Gonder
Date: Wed, 31 Oct 2007 16:14
Date: Wed, 31 Oct 2007 16:14
27 lines
2152 bytes
2152 bytes
Patryk B. wrote:
>Im traying to prevent to close my application.
>I know its function OnCloseQuery but i want to prevent closing
> from Task Manager too.
>In know that global hook should be placed in DLL.
>But I want hook only for my application.
Then you should try hooking in WinMain instead of TForm6::Button1Click
And be aware that non-DLL hooks are for one thread only.
>What is wrong? It dont detect WM_QUIT.
Could it be that VCL doesn't post that message?
In any case, the window will have already been destroyed (WM_DESTROY)
before WM_QUIT is posted. Also, WM_DESTROY comes _after_ the window is destroyed.
It would probably be easier to just place a while(1){} around
the application code in WinMain, than to try to stop WM_QUIT.
I think you want to catch it even sooner.
You might look into WM_SYSCOMMAND-SC_CLOSE
>LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
I'm not sure what the ncode is for WM_QUIT, but it might be other than HC_ACTION.
You might check all messages.
It might also be a good idea to use the WinAPI instead of VCL code in the handler.
Re: WM_QUIT and Hook
Author: 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads