Problem with SetWindowsHookEx

Started by
2 comments, last by Amr0 12 years, 3 months ago
Hey everyone

I try to get notified when a new window is created. To accomplish that i use a global windows hook like that:


LRESULT CALLBACK ShellHookProc(int code, WPARAM wParam, LPARAM lParam)
{
if(code == HSHELL_WINDOWCREATED) {
HWND hWindow = (HWND)wParam;
WCHAR wndText[255];
GetWindowText(hWindow, wndText, 255);
MessageBox(nullptr, L"WindowCreated", wndText, MB_OK);
}
return CallNextHookEx(nullptr, code, wParam, lParam);
}

__declspec(dllexport) void __stdcall initHooking()
{
HHOOK gShellHook = SetWindowsHookEx(WH_SHELL, ShellHookProc, GetModuleHandle(L"HookDll.dll"), 0);
if(gShellHook == nullptr) {
std::wstringstream strm;
strm << L"Error setting hook: " << GetLastError();
MessageBox(nullptr, strm.str().c_str(), L"", MB_OK);
}
}


The hook actually works, but it gets called like 10 times for the same window with the same window handle. Is that normal? And if so whats the reason?

Greetings
Muepe
Advertisement
return CallNextHookEx(nullptr, code, wParam, lParam);
Don't pass nullptr to CallNextHookEx(). Instead pass gShellHook.
Isn't the first argument to CallNextHookEx() ignored?
Oh... apparently it is! Could this be a change from what was in the past?

Also, consider that in the hook procedure you create a message box, which is a window, which has buttons which are also windows.

This topic is closed to new replies.

Advertisement