Solved

Why my keyboard hook does not work?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

914 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

13 Experts available now in Live!

Get 1:1 Help Now