Solved

Why my keyboard hook does not work?

Posted on 1998-11-10
3
296 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

789 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