Martin
Martin

Reputation: 35

c++ global hooking. Why SetWindowsHookEx return NULL?

I have a big problem with SetWindowsHookEx and WH_CALLWNDPROC! When use SetWindowsHookEx for local hook

HHOOK hook = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, GetModuleHandle(NULL), GetCurrentThreadId());

all work fine, but when i try set global hook or hook for other program:

HHOOK hook = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, GetModuleHandle(NULL), threadId);

where threadId = GetWindowThreadProcessId(window, NULL);
and HWND window = ::FindWindow(NULL,"Program title");

i get NULL in hook...

I read very much for hooking but, i don't understand where is worng... I have a headache for a few days. Thank you in advance and sorry for my english.

Upvotes: 3

Views: 2772

Answers (1)

manuell
manuell

Reputation: 7620

One way to do it:

  1. Put your CallWndProc function in a DLL
  2. Export that function using a def file with the following content:

LIBRARY

EXPORTS

    CallWndProc
  1. Done!
  2. (Try to always use GetLastError when a Win32 API Fails, and give the result when you ask something)

Upvotes: 2

Related Questions