4

I'm trying to set a WH_CBT hook, and it return 0 all the time. I checked for error, and I got error 1428. I researched a little and found out that I have a problem with the hMod parameter, though I can't see what should I put in it instead of null. Does anyone know what I am doing wrong?

This is my code:

#include "stdafx.h"
#include "Windows.h"
#include <iostream>
using namespace std;

HHOOK hookHandle;

LRESULT CALLBACK CBTProc( int nCode,  WPARAM wParam,  LPARAM lParam);

int _tmain(int argc, _TCHAR* argv[]) 
{
hookHandle = SetWindowsHookEx(WH_CBT,CBTProc,NULL,0);

 if(hookHandle == NULL) 
 {
  cout << "ERROR CREATING HOOK: ";
  cout << GetLastError() << endl;
  getchar();
  return 0;
 }

 MSG message;

 while(GetMessage(&message, NULL, 0, 0) != 0) 
 {
  TranslateMessage( &message );
  DispatchMessage( &message );
 }



 cout << "Press any key to quit...";
 getchar();

 UnhookWindowsHookEx(hookHandle);

 return 0;
}

LRESULT CALLBACK CBTProc( int nCode,WPARAM wParam, LPARAM lParam)
{
cout << "hello" << endl;
    return CallNextHookEx(hookHandle, nCode, 
            wParam, lParam);
}

P.S. I apologize if the code has stupid elements about it. I'm not a newbie to programming, just to C++.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Oran
  • 121
  • 2
  • 3

3 Answers3

3

If you specify 0 for the threadid that specifies the hook to be global. For that to work, the hook needs to be injected into other processes. This means the hook needs to be exposed from a DLL. You need to either move the hook procedure to a dll, or specify a thread in your process.

Logan Capaldo
  • 39,555
  • 5
  • 63
  • 78
  • hey, just a question, is it possible to do the hook installation within the .dll (where the hook proc is at) ? –  Apr 11 '13 at 22:46
2

Use GetModuleHandle(NULL) and GetCurrentThreadId() to get the handle and the thread id you need to pass to that function.

Sample:

hookHandle = SetWindowsHookEx(WH_CBT,CBTProc,
                              GetModuleHandle(NULL),  
                              GetCurrentThreadId());

As Logan says, that would hook only the current process. You need to put the code in a dll to develop a system hook.

Jesus Oliva
  • 2,282
  • 14
  • 18
  • how? can you give me an example please? – Oran Dec 08 '10 at 14:29
  • check passing the current thread id as Cody said. But remember this hook only will capture messages for its same process... I have updated the answer. – Jesus Oliva Dec 08 '10 at 14:35
  • thanks, but what do i have to do to capture messages for all of the processes currently running? – Oran Dec 08 '10 at 14:38
  • Good answer from stackoverflow: http://stackoverflow.com/questions/454477/global-hook-setwindowshookex – Jesus Oliva Dec 08 '10 at 14:40
  • GetModuleHandle(NULL) cannot work here, this is an EXE. Getting the code into a DLL is very necessary to get this going. – Hans Passant Dec 08 '10 at 15:14
0

I know that this is very old post, but I was fighting with the simmilar issue. I wanted to track size and location changes for the "Shell_traywnd" window and I found a solution in this thread. I belive that it will help for someone else.

Paweł Iwaneczko
  • 853
  • 1
  • 10
  • 13