Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why my keyboard hook does not work?

Posted on 1998-11-10
3
Medium Priority
?
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 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…
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…

598 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