Why does SetWindowsHookEx return 0?

I am trying to set the WH_CBThook and it returns 0 all the time. I checked the error and I got error 1428. I did a little research and found out that I have a problem with the parameter hMod, although I do not see what I should insert instead null. Does anyone know what I'm 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);
}

PS I apologize if there are stupid elements in the code. I am not new to programming, just C ++.

+3
source share
2 answers

0 , , hook . . , DLL. hook dll, .

+3

GetModuleHandle(NULL) GetCurrentThreadId(), , .

:

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

, . dll, .

+1

Source: https://habr.com/ru/post/1779189/


All Articles