From: ama on
hi

here we go again with another question about system hooks...

I compiled a dll and exported a function using a .DEF file.
lets call it lib.dll .

Inside my .exe i load the library and i call the exported call
StartHook (see code below)

Now the question i have is : What do i need to do to
get notified for HCBT_CREATEWND for other applications ?

Right now the only time CBTProc is called is for MY application
only and of course i was hoping for a system wide hook so i can
be notified when ANY window is created.

Funny thing is i didnt tell SetWindowsHookEx to hook a specific Process.
So what 's going on here? If this is default behavior whats the
alternative.

Here's part of the dll code :

HHOOK gHhook = NULL;
HINSTANCE gHinstance = NULL;

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved)
{
gHinstance = (HINSTANCE)hModule;
return TRUE;
}

LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if( nCode < 0 )
return CallNextHookEx(gHhook, nCode, wParam, lParam);

CallNextHookEx(gHhook, nCode, wParam, lParam);

if( nCode == HCBT_CREATEWND )
::PostMessage(ghTarget, WM_USER_WINDOWCREATED, wParam, 0);

return 0;
}

//exported call
HHOOK APIENTRY StartHook(HWND htarget)
{
if( ! gHinstance )
return 0;

ghTarget = htarget;
gHhook = SetWindowsHookEx(WH_CBT, (HOOKPROC)&CBTProc, gHinstance, 0);

return gHhook;
}







From: Igor Tandetnik on
ama <a.m.a(a)videotron.ca> wrote:
> I compiled a dll and exported a function using a .DEF file.
> lets call it lib.dll .
>
> Inside my .exe i load the library and i call the exported call
> StartHook (see code below)
>
> Now the question i have is : What do i need to do to
> get notified for HCBT_CREATEWND for other applications ?

Most likely, you already do. The problem is, each mapping of a DLL in a
separate process has its own set of global variables. StartHook sets
gHhook and ghTarget in the process it is called from, but these values
are not visible in any other process. Your hook gets called from other
processes but is unable to report this fact.

See http://www.codeproject.com/dll/hooks.asp, the part that talks about
#pragma data_seg. Note that you do _not_ want to share gHinstance, but
you need to share gHhook and ghTarget.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


From: James Brown on
comments inline

"ama" <a.m.a(a)videotron.ca> wrote in message
news:pMzsf.123974$0p5.1325182(a)wagner.videotron.net...
> hi
>
> here we go again with another question about system hooks...
>
> I compiled a dll and exported a function using a .DEF file.
> lets call it lib.dll .
>
> Inside my .exe i load the library and i call the exported call
> StartHook (see code below)
>
> Now the question i have is : What do i need to do to
> get notified for HCBT_CREATEWND for other applications ?
>
> Right now the only time CBTProc is called is for MY application
> only and of course i was hoping for a system wide hook so i can
> be notified when ANY window is created.
>
> Funny thing is i didnt tell SetWindowsHookEx to hook a specific Process.
> So what 's going on here? If this is default behavior whats the
> alternative.
>
> Here's part of the dll code :
>
> HHOOK gHhook = NULL;
> HINSTANCE gHinstance = NULL;


You *must* place your gHhook handle inside a shared section. You haven't
made the mistake that most make by failing to initialize it to NULL though
(failure to do this means it won't get placed in your new section).

#pragma data_seg(".shared")
#pragma comment(linker, "/section:.shared,rws")
HHOOK gHook = NULL;
HWND ghTarget = NULL;
#pragma data_seg()


> BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID
> lpReserved)
> {
> gHinstance = (HINSTANCE)hModule;
> return TRUE;
> }
>
> LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam,LPARAM lParam)
> {
> if( nCode < 0 )
> return CallNextHookEx(gHhook, nCode, wParam, lParam);
>
> CallNextHookEx(gHhook, nCode, wParam, lParam);
>
> if( nCode == HCBT_CREATEWND )
> ::PostMessage(ghTarget, WM_USER_WINDOWCREATED, wParam, 0);
>
> return 0;
> }
>
> //exported call
> HHOOK APIENTRY StartHook(HWND htarget)
> {
> if( ! gHinstance )
> return 0;
>
> ghTarget = htarget;
> gHhook = SetWindowsHookEx(WH_CBT, (HOOKPROC)&CBTProc, gHinstance, 0);
>
> return gHhook;
> }
>
>
>

The docs are very clear about global hooking. If you pass 0 (NULL) as the
last
parameter to SetWindowsHookEx then your hook is associated with all threads
running
under the current desktop (and therefore will be automatically injected into
any process which
has it's threads hooked)

When your CBTProc executes it will be in the context of another process. So
you
will need to devise some mechanism to communicate back to the "original"
process.

I see you are using PostMessage for this matter. This is a good choice, but
be careful
that you also place your "ghTarget" window-handle in the ".shared" section,
otherwise this window-handle will not be accessible to other processes which
map
your DLL into their address spaces.



James
--
www.catch22.net
Free Win32 Source and Tutorials


From: ama on

> Most likely, you already do. The problem is, each mapping of a DLL in a
> separate process has its own set of global variables. StartHook sets
> gHhook and ghTarget in the process it is called from, but these values are
> not visible in any other process. Your hook gets called from other
> processes but is unable to report this fact.
>

Ok thanks.

I tried all that too :

#pragma data_seg(".shared")
HHOOK gHhook = NULL;
HINSTANCE gHinstance = NULL;
UINT WM_USER_WINDOWCREATED = (WM_USER+100);
#pragma data_seg()
#pragma comment( linker, "/SECTION:.shared,RWS" )

before and after posting the first question. I tried everything
in case there's a flaw in the docs or with winxp sp2 etc...

Since noone pointed me with errors in the code posted
in the previous post and if the code above is ok
ill assume the problem is something wrong with windows
on my PC... lets call it voodoo..

thanks anyways.




From: Alexander Grigoriev on
Read the thread again. You don't need to share gHinstance, but you need
ghTarget.

"ama" <a.m.a(a)videotron.ca> wrote in message
news:ScIsf.131591$0p5.1588261(a)wagner.videotron.net...
>
>> Most likely, you already do. The problem is, each mapping of a DLL in a
>> separate process has its own set of global variables. StartHook sets
>> gHhook and ghTarget in the process it is called from, but these values
>> are not visible in any other process. Your hook gets called from other
>> processes but is unable to report this fact.
>>
>
> Ok thanks.
>
> I tried all that too :
>
> #pragma data_seg(".shared")
> HHOOK gHhook = NULL;
> HINSTANCE gHinstance = NULL;
> UINT WM_USER_WINDOWCREATED = (WM_USER+100);
> #pragma data_seg()
> #pragma comment( linker, "/SECTION:.shared,RWS" )
>
> before and after posting the first question. I tried everything
> in case there's a flaw in the docs or with winxp sp2 etc...
>
> Since noone pointed me with errors in the code posted
> in the previous post and if the code above is ok
> ill assume the problem is something wrong with windows
> on my PC... lets call it voodoo..
>
> thanks anyways.
>
>
>
>