System wide Message Hook not working when caller app loses focus 
Author Message
 System wide Message Hook not working when caller app loses focus

I am trying to write a System wide Keyboard hook.  I've
got the attached code working when my calling app (C#
winform) has the focus.  In that case I get log entries in
hook_log.txt saying Keyboard Callback.  That's great but I
have two problems.  

1. If I change focus to another application (ie Notepad)
and type I never see any log entries from the keyboard
message handler.

2. If I go back to my calling app and type I don't always
get log entries.  To get it to work again I have to UnSet
the hook and reset it.

Does anyone have experience with this?  Any help would be
greatly appreciated.

Here's a sample from the log.  All of these entries are
from my caller app.  

255760774 - DllMain called
255760784 - Process attaching
255760784 - g_hInstance is NOT null
255763578 - Keyboard Callback
255763688 - Keyboard Callback
255763858 - Keyboard Callback
255764009 - Keyboard Callback
255764219 - Keyboard Callback
255764319 - Keyboard Callback
...

  IdleTracker.cpp
4K Download


Sun, 31 Oct 2004 21:45:21 GMT  
 System wide Message Hook not working when caller app loses focus


Quote:
>I am trying to write a System wide Keyboard hook.  I've
>got the attached code working when my calling app (C#
>winform) has the focus.  In that case I get log entries in
>hook_log.txt saying Keyboard Callback.  That's great but I
>have two problems.

>1. If I change focus to another application (ie Notepad)
>and type I never see any log entries from the keyboard
>message handler.

>2. If I go back to my calling app and type I don't always
>get log entries.  To get it to work again I have to UnSet
>the hook and reset it.

Is the hook in a DLL or the EXE? System wide
hooks have to be in a DLL.

How are you logging to the file? Every instance
of the DLL would need its own handle to the
file.



Mon, 01 Nov 2004 01:52:25 GMT  
 System wide Message Hook not working when caller app loses focus
Ron, I'm using a dll.  I attached the main class to my original post.

Here's my logging function.  It's just for debug purposes so there might be
problems with it but it works.

Any ideas why my dll wouldn't work?

Greg

BOOL LogToFile(LPSTR lpbyContentBuf)

{

HANDLE hDataFile; // handle to content file

DWORD dwBytesWritten; // used with WriteFile

//char szFilePath[MAX_PATH+1];

char szLogFile[MAX_PATH+1];

sprintf(szLogFile, "hook_log.txt");

// Create File

hDataFile = CreateFile(szLogFile,

GENERIC_WRITE,

0, // No sharing mode

NULL, // Default security attribs

OPEN_ALWAYS,

FILE_ATTRIBUTE_NORMAL,

NULL // No template file

);

if (hDataFile == INVALID_HANDLE_VALUE)

{

//File Not Created

return FALSE;

Quote:
}

BOOL bolReturn = SetFilePointer(hDataFile, 0, 0, SEEK_END);

char szCurrentTime[20];

ltoa(GetTickCount(), szCurrentTime, 10);

char szLog[MAX_PATH];

memset(szLog, 0, sizeof(szLog));

wsprintfA(szLog, "%s - %s\r\n", szCurrentTime, lpbyContentBuf);

bolReturn = WriteFile( hDataFile, szLog, strlen(szLog), &dwBytesWritten,
NULL );

CloseHandle( hDataFile );

return bolReturn;

//#else

// return TRUE;

//#endif

Quote:
}



Quote:



> >I am trying to write a System wide Keyboard hook.  I've
> >got the attached code working when my calling app (C#
> >winform) has the focus.  In that case I get log entries in
> >hook_log.txt saying Keyboard Callback.  That's great but I
> >have two problems.

> >1. If I change focus to another application (ie Notepad)
> >and type I never see any log entries from the keyboard
> >message handler.

> >2. If I go back to my calling app and type I don't always
> >get log entries.  To get it to work again I have to UnSet
> >the hook and reset it.

> Is the hook in a DLL or the EXE? System wide
> hooks have to be in a DLL.

> How are you logging to the file? Every instance
> of the DLL would need its own handle to the
> file.



Mon, 01 Nov 2004 11:28:06 GMT  
 System wide Message Hook not working when caller app loses focus
All, the problem seems to be that the caller app was written in C# and
therefore running as managed code.  I wrote a C++ command line util to call
the same dll and it worked great.

--
Greg
http://www.claritycon.com/


Quote:
> I am trying to write a System wide Keyboard hook.  I've
> got the attached code working when my calling app (C#
> winform) has the focus.  In that case I get log entries in
> hook_log.txt saying Keyboard Callback.  That's great but I
> have two problems.

> 1. If I change focus to another application (ie Notepad)
> and type I never see any log entries from the keyboard
> message handler.

> 2. If I go back to my calling app and type I don't always
> get log entries.  To get it to work again I have to UnSet
> the hook and reset it.

> Does anyone have experience with this?  Any help would be
> greatly appreciated.

> Here's a sample from the log.  All of these entries are
> from my caller app.

> 255760774 - DllMain called
> 255760784 - Process attaching
> 255760784 - g_hInstance is NOT null
> 255763578 - Keyboard Callback
> 255763688 - Keyboard Callback
> 255763858 - Keyboard Callback
> 255764009 - Keyboard Callback
> 255764219 - Keyboard Callback
> 255764319 - Keyboard Callback
> ...



Tue, 02 Nov 2004 03:25:29 GMT  
 System wide Message Hook not working when caller app loses focus

Quote:

>Ron, I'm using a dll.  I attached the main class to my original post.

>Here's my logging function.  It's just for debug purposes so there might be
>problems with it but it works.

>Any ideas why my dll wouldn't work?

Looks generally good. A couple things I see
that could be a problem.

1 - you are using a relative path for the
name of the log file. Were you intending to
create a different log file for each application
your hook is loaded into? The given code
would use the current directory of each process,
I believe. This should generate a different log
file for each application that loads the DLL,
unless the application's current directory
happens to be the same as another application.

2 - If an application which loads your hook
runs, then exits, it will unhook the hook on
DLL_PROCESS_DETACH, regardless of
whether it set the hook in the first place.

This could be really interesting if you load
an application like notes, which runs a stub
that loads another program and exits
immediately.

You might try adding a non-shared variable
to ensure only the instance that set the
hook unhooks it.

That's all that strikes me offhand.



Tue, 02 Nov 2004 03:47:23 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to install System Wide Hooks, Sample code included working for installing app

2. How to make a system-wide message hook ?

3. system wide keyboard hook in MFC app

4. Creating system wide keyboard Hooks with c#

5. system wide hook

6. Obtaining HWND in system wide hook DLL?

7. How to setup system-wide hook for Common Dialogs

8. System wide hook problem...

9. Hooking system wide

10. ShellExecute() seems to unhook system wide hook...

11. System wide hook question...

12. system-wide hook?

 

 
Powered by phpBB® Forum Software