Solved

Why my keyboard hook does not work?

Posted on 1998-11-10
3
267 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now