tags:

views:

126

answers:

3

Hi,

We are hooking TextOut(),ExtTextOut() and DrawText() methods GLOBALLY .

i.e.

hhook = SetWindowsHookEx(WH_CBT, function_address, module_handle, 0);

But we want to exclude our application (which we are using to install/uninstall hook) from being hooked. If the last argument to SetWindowsHookEx() is 0(zero) it will hook all the existing threads.How to check here if the current thread is "OurApplication.exe" and then exclude it from hooking or immediately unhook it. Please provide help.

A: 

I don't think it's possible. You either hook to everything or to a specific thread. Why don't you just filter out your application in whatever code yout have at function_address? Most, if not all, CBT hook callbacks provide window handle at either wParam or lParam argument. You can then get process id from that handle and compare it to your application pid.

Chriso
A: 

Off the top of my head:

Pass the hook dll the PID of the process you want to ignore when you install the hook. Make sure that PID is stored in a shared section so all hook instances see the same value.

In your hook function, check to see if the current process PID matches the one passed in. If it does, don't do your hooky stuff, just pass to CallNextHookEx.

I don't like this because it adds to work done in the hook function, which is always bad. But it seems like it should work in principle.

Bob Moore
A: 

Thank you experts for replying to our question. We found the way to do that. Now we added the following block of code in the entry point of the injecting dll.And it is working fine.

BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD dwReason, PVOID lpReserved) 
{   
    switch (dwReason) 
    {
       case DLL_PROCESS_ATTACH:
           IsDebuggerPresent();

           // Exclude the "someapplication.exe" from hooking
           GetModuleFileName( GetModuleHandle( NULL ),Work,sizeof(Work) );
           PathStripPath(Work );

           if ( _stricmp( Work, "someapplication.exe" ) != 0 )
           {
              InstallWindowHooks();
           }

         break;
       case DLL_PROCESS_DETACH:
           hWindowReceiver = NULL;
           CleanUp();
         break;     
    }
    return TRUE;
}
team-ferrari22