Re: Preventing a program from gaining the focus with a CBT hook
- From: "Carlton Craighead" <carlton@xxxxxxxxxxxxxxxx>
- Date: Thu, 6 Mar 2008 00:25:00 -0800
Thanks for replying Tim.
Why the hook...
Not for fun, just to keep a devtool (CodeWarrior) from stealing focus as it
batch builds a big project.
Giving focus back to the losing window...
I should have realized to at least try this. But I have a hunch it will not
work, as I don't think the loser has lost it yet. But I'll give it a try.
Hooks being intrusive / DLL unload issues...
Yes I am aware of this. I know they are bad drugs. Fortunately I find the
DLL unloads without too much trouble. I'm looking at alternative ways of
doing this, possibly using desktops.
"nCode >= 0" is useless
Yes, I know. I left it there for documentation purposes.
Why not "memset( caption, 0, sizeof(caption) )"?
And why not "if( _tcscmp( caption, _T("Calculator") ) == 0 )"?
I believe these functions are not thread safe. I actually had them coded
just as you wrote. It simply did not work. With the immediate data there,
no problem. And I'm sure this is faster (a minor point).
Thanks again.
C
"Tim Roberts" <timr@xxxxxxxxx> wrote in message
news:m1jss3to88iecgu1c86mfsm28gts1kekr4@xxxxxxxxxx
"Carlton Craighead" <carlton@xxxxxxxxxxxxxxxx> wrote:
I'm trying to prevent a program (using Calculator as a test) from gaining
the focus.
Why? For fun? You can't rely on this as a security technique.
I've set up a CBT hook and have all the code in place. I get the callback
indicating that its about to get the focus, but returning 1 does not
prevent it, as the documentation says it should. I had replaced that
"return 1" with a "MessageBeep(0)" and know that the CBT callbacks are
being received.
Perhaps you should try giving focus back to the window that's about to
lose
it. The losing window handle is in the LPARAM.
It seems that I can only install this hook once. If I install it, it
works
great; then uninstall, with no error. If I install it again (with that
same program still running that did the first one), it does not work (no
CBT callbacks). Uninstalling it then produces an invalid hook handle
(1404).
Right. It is easy to forget how utterly intrusive a system hook is. When
you install a system hook like this, your DLL is literally "injected" into
every running process, and every process that starts from that time on.
Every process will load and execute a copy of your DLL. The operating
system cannot always safely UNLOAD your DLL from every process when you
drop the hook, so the DLL is still loaded, and cannot easily be replaced.
You may find that you have to reboot quite often during hook development.
extern "C" LRESULT __stdcall CBTHookProc(int nCode, WPARAM wParam, LPARAM
lParam)
{
if (nCode >= 0 && nCode == HCBT_SETFOCUS)
You realize that the "nCode >= 0" is useless here, right?
for (int i = 0; i < MAXCAPTIONSIZE; i++)
caption[i] = '\0';
Why not "memset( caption, 0, sizeof(caption) )"?
GetWindowText((HWND) wParam, caption, sizeof(caption) - 1);
if (caption[0] == 'C' &&
caption[1] == 'a' &&
caption[2] == 'l' &&
caption[3] == 'c' &&
caption[4] == 'u' &&
caption[5] == 'l' &&
caption[6] == 'a' &&
caption[7] == 't' &&
caption[8] == 'o' &&
caption[9] == 'r')
{
return 1;
}
}
And why not "if( _tcscmp( caption, _T("Calculator") ) == 0 )"?
--
Tim Roberts, timr@xxxxxxxxx
Providenza & Boekelheide, Inc.
.
- Follow-Ups:
- Re: Preventing a program from gaining the focus with a CBT hook
- From: Ben Voigt [C++ MVP]
- Re: Preventing a program from gaining the focus with a CBT hook
- References:
- Preventing a program from gaining the focus with a CBT hook
- From: Carlton Craighead
- Re: Preventing a program from gaining the focus with a CBT hook
- From: Tim Roberts
- Preventing a program from gaining the focus with a CBT hook
- Prev by Date: Re: How Can I DeviceIoControl in less than 10 milliseconds?
- Next by Date: Re: How Can I DeviceIoControl in less than 10 milliseconds?
- Previous by thread: Re: Preventing a program from gaining the focus with a CBT hook
- Next by thread: Re: Preventing a program from gaining the focus with a CBT hook
- Index(es):
Relevant Pages
|