Jump to content
  • Advertisement
Sign in to follow this  
YanickSalzmann

Problem with SetWindowsHookEx

This topic is 2543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
return CallNextHookEx(nullptr, code, wParam, lParam);
Don't pass nullptr to CallNextHookEx(). Instead pass gShellHook.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!