Tried a bunch of things but I can't get it to work consistently amid my taskbar being nuked and other supernatural effects on my desktop UI.
Tried using a open-library http://mwinapi.sourceforge.net/ first. Although it worked nicely as an OO layer for enumerating windows and stuff. It couldn't do hooks properly
Next stop was Dino E.'s post on Windows Hooks in the .Net framework. I ended up writing my own type as I was understanding the text and trying to get this to work.
My intention is to have this app running and have it be able to log all created windows while it is running. Calling all eyeballs...
Update: Snipped since apparently you can't write global windows hooks in .Net / managed code (except some low level mouse or keyboard hooks)
So I switched to C++. Still all WinAPI calls return valid handles but I don't see my filter function being called - don't seem to be receiving any notifications. Still doesn't work... Can someone spot the mistake.
void CWinHookFacade::Hook()
{
HMODULE hCurrentDll = LoadLibrary(_T("[Path to my hook dll]"));
m_HookHandle = SetWindowsHookEx(WH_CBT,
FilterFunctionForHook,
hCurrentDll,
0);
if (m_HookHandle == NULL)
{
throw new std::exception("Unable to hook");
}
}
void CWinHookFacade::Unhook()
{
if (!UnhookWindowsHookEx(m_HookHandle))
{
throw new std::exception("Unhook failed!");
}
m_HookHandle = NULL;
}
LRESULT CWinHookFacade::FilterFunctionForHook(int code, WPARAM wParam, LPARAM lParam)
{
if (code >= 0)
{
switch(code)
{
case HCBT_CREATEWND:
wprintf(_T("Created Window"));
break;
case HCBT_ACTIVATE:
wprintf(_T("Activated Window"));
break;
case HCBT_DESTROYWND:
wprintf(_T("Destroy Window"));
break;
}
}
return CallNextHookEx(m_HookHandle, code, wParam, lParam);
}
Client exe calls the Hook_DLL like this
int _tmain(int argc, _TCHAR* argv[])
{
CWinHookFacade::Hook();
getchar();
CWinHookFacade::Unhook();
}