Article ID: 179905
Article Last Modified on 7/11/2005
#ifdef _DEBUG
#define PATHSIZE 256
BOOL IsCurProcDebugger();
#endif
// Shared DATA
#pragma data_seg("HOOKVARS")
// Other variables should be in here (for example, hhookKeyboard).
// Add this variable to the shared data segment.
#ifdef _DEBUG
DWORD dwDebuggerProcId = 0;
#endif
#pragma data_seg()
// Filter function for the WH_KEYBOARD.
LRESULT CALLBACK KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam )
{
if ( nCode >= 0 )
{
// Add this if-block to your code.
//*** You should put breakpoints ONLY inside this if-block ***
#ifdef _DEBUG
if ( !IsCurProcDebugger() ) // If you are not in the debugger...
{
#endif
// Your hook code goes here.
#ifdef _DEBUG
}
#endif
}
return( CallNextHookEx(hhookKeyboard, nCode, wParam, lParam));
}
#ifdef _DEBUG
// *** DO NOT PUT ANY BREAKPOINTS IN THIS CODE!!!
BOOL IsCurProcDebugger()
{
// In Visual C++, declare these three variables after the
// following if-block to speed things up.
char szPathName[PATHSIZE];
char *szFileName = szPathName;
char *tcp;
// Do this first for speed.
// If there is only one debugger present
// and you have already found it.
if ( dwDebuggerProcId )
{
return ( GetCurrentProcessId() == dwDebuggerProcId );
}
// If only one debugger is running, then the rest of this code
// should be entered only until the debugger is first hooked.
// After that, the preceding code should catch it every time.
GetModuleFileName( NULL, szPathName, PATHSIZE );
// Only check the file name, not the full path.
// A co-worker's path may be different.
for ( tcp = szPathName; *tcp; tcp++ )
{
if ( *tcp == '/' || *tcp == '\\' )
szFileName = tcp + 1;
}
// Use "MSDEV.EXE" for the Visual C++ debugger, or
// else use YOUR debugger's name.
if ( !strcmp( _strupr(szFileName), "MSDEV.EXE") )
{
// It's the debugger!
dwDebuggerProcId = GetCurrentProcessId();
return TRUE;
}
return FALSE;
}
#endif // #ifdef _DEBUG
125677 How To Share Data Between Different Mappings of a DLL
100292 PRB: Data Section Names Limited to Eight Characters
102428 How To Debug a System-Wide Hook
Keywords: kbhowto kbhook KB179905