Solved

Why my keyboard hook does not work?

Posted on 1998-11-10
3
289 Views
Last Modified: 2008-01-16
I wrote a keyboard hook dll, but I can not get my breakpoint
at hook proc. My dll project is produced by appwizard.
That's my code:

__declspec(dllexport) LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
      if (nCode < 0)
            return CallNextHookEx(hKeyHook, nCode, wParam, lParam);

      if (nCode == HC_NOREMOVE)
            return CallNextHookEx(hKeyHook, nCode, wParam, lParam);

      if (wParam == 'a' || wParam == 'A')
            return TRUE;
      else
            return CallNextHookEx(hKeyHook, nCode, wParam, lParam);
}

_declspec(dllexport) BOOL InstallKbHook()
{
      lpfnHookProc = (HOOKPROC)KeyboardProc;
   
      hKeyHook = SetWindowsHookEx(WH_KEYBOARD, lpfnHookProc, hInstance,
            NULL);
   
      if (hKeyHook)
            return TRUE ;
      else
            return FALSE ;
}

_declspec(dllexport) void UnInstallKbHook()
{
      UnhookWindowsHookEx(hKeyHook);
}

BOOL CKbHookApp::InitInstance()
{
      hInstance = GetModuleHandle(NULL);

      return CWinApp::InitInstance();
}

Note that hKeyHook is a shared data.
In my exe file, I call InstallKbHook(), but even I press 'a'
key, my KeyboardProc is not called.

Who can tell me why?

Thanks in advance.
0
Comment
Question by:wuxz
3 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1177503
Hmmm...  It's difficult to say without seeing the whole code.
Some notes:

>> I wrote a keyboard hook dll, but I can not get my breakpoint at hook proc.
You cannot put a breakpoint in your hook proc because the DLL is injected into aall the running processes and you're only debugging one of them.  Use OutputDebugString() or MessageBox() instead.

>> My dll project is produced by appwizard.
That can complicate things.  I'd stick to plain DLL and avoid Wizards and MFC when hooks are concerned.

Make sure your KeyboardProc() is declared as extern "C" to prevent name mangling.

>> if (wParam == 'a' || wParam == 'A')
>>    return TRUE;
You should always CallNextHookEx(), regardless of the code.

>> Note that hKeyHook is a shared data.
Show the code.

>> BOOL CKbHookApp::InitInstance()
>> {
>>   hInstance = GetModuleHandle(NULL);
>>   return CWinApp::InitInstance();
>> }
Seems a bit fishy to me.  Put a breakpoint there to see if it gets executed.

Also check the following discussions:
    http://www.experts-exchange.com/Q.10063288 (5 points)
    http://www.experts-exchange.com/Q.10069661 (4 points)

0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 1177504
You _cannot_ set a breakpoint in a hook proc from within DevStudio. Assuming it is a system wide hook, the solution is like this:

// in your DLL
// link with SECTIONS .shared READ WRITE SHARED in .def
#pragma data_seg( ".shared")
DWORD g_dwPIDToBreak = 0;
#pragma data_seg()
//...
_declspec(dllexport) BOOL InstallKbHook()
{
 g_dwPIDToBreak = GetCurrentProcessId();
//...
}

__declspec(dllexport) LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
return CallNextHookEx(hKeyHook, nCode, wParam, lParam);
//...

if ( g_dwPIDToBreak == GetCurrentProcessId())
{
__asm { int 3};
}
}

This will emit an user breakpoint only in the context of your application that set the hook (in order to avoid a breakpoint in all other programs, like e.g. explorer.exe)
0
 
LVL 2

Author Comment

by:wuxz
ID: 1177505
Sorry, guys

I have resolved my problem. I am sorry for not comment it early.
Thanks for all of you. I think this question can be closed.

Thanks anyway.

Regards
wu xz
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question