Re: Preventing a program from gaining the focus with a CBT hook

Tech-Archive recommends: Fix windows errors by optimizing your registry



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.


.



Relevant Pages

  • Re: Preventing a program from gaining the focus with a CBT hook
    ... Giving focus back to the losing window... ... Hooks being intrusive / DLL unload issues... ... Why not "memset(caption, 0, sizeof(caption))"? ... It is easy to forget how utterly intrusive a system hook is. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Using Visual Basic to Write System-Wide Hooks
    ... >I've spent about an hour looking into writing a system-wide hook using only ... a system-wide callback function must be located within a DLL. ... >system-wide hook DLL. ... The reason is that Visual Basic has some large runtime ...
    (microsoft.public.vb.winapi)
  • Re: SetWindowsHookEx
    ... automatically inject into all programs. ... It sets the global hook on the ... DLL injecting - in all likelyhook SetWindowsHook returns before the DLL has ... with your shared-section, then the gHook variable (which contains ...
    (microsoft.public.vc.language)
  • Re: Writing to a file within a Call Back Function
    ... the fact it is a CALLBACK function and EVERYTHING to do with the fact it is a hook ... within a DLL file that I created. ... SetWindowsHookEx(..) which MSDN says will set a hook on this function ... The most common way to handle this is to put a *window* handle in a shared data segment. ...
    (microsoft.public.vc.mfc)
  • Re: RAFS Rendesvous
    ... The powers that be in Naval Air are wisely ensuring that at least one air wing is scheduled for workups at NAS Fallon during 'Hook, allowing busloads of youngsters to trek the 50 miles into Reno for the fun. ... "Snow" is a four-letter word and should be treated like all othe four ...
    (rec.aviation.military.naval)