Re: Preventing a program from gaining the focus with a CBT hook
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Thu, 6 Mar 2008 08:40:38 -0600
Carlton Craighead wrote:
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).
memset will be faster than your loop (the compiler may actually recognize
the loop and replace it with memset). I'm not sure you even need to clear
the buffer at all though.
Your comparison can be trivially sped up by 100%-300% by using 32-bit
compares instead.
And memset and strcpy are both threadsafe if you are not using the same
buffer from another thread, since you have a stack buffer that condition is
guaranteed (barring wild/dangling pointers in other areas of the program).
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.
.
- 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
- Re: Preventing a program from gaining the focus with a CBT hook
- From: Carlton Craighead
- Preventing a program from gaining the focus with a CBT hook
- Prev by Date: Re: How detect Server 2008 Core?
- Next by Date: Re: dump NTFS volume in Vista
- Previous by thread: Re: Preventing a program from gaining the focus with a CBT hook
- Next by thread: My reader-writer lock class
- Index(es):
Relevant Pages
|
Loading