I'm trying to hook into Windows' sizing events. I wrote the following hook:
__declspec(dllexport)
LRESULT CALLBACK HookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
{
MSG *msg = (MSG *)lParam;
wchar_t text[1014];
GetWindowText(msg->hwnd, text, 1024);
if (wcscmp(text, L"Untitled - Notepad") == 0)
{
if (nCode == HCBT_MOVESIZE)
{
FILE *file;
fopen_s(&file, "C:\\Users\\Me\\hooklog.txt", "a+");
fprintf(file, "Move or size.\n");
fclose(file);
}
else
{
FILE *file;
fopen_s(&file, "C:\\Users\\Me\\hooklog.txt", "a+");
fprintf(file, "Something else.\n");
fclose(file);
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
And I'm tring to install it using:
HHOOK hhk = SetWindowsHookEx(WH_CBT, hookProc, hModule, 0);
DWORD err = GetLastError();
The same technique works for other types of hooks, but fot WH_CBT is not working. All the variables (hookProc, hModule ad hhk after the call, and err is 0) involved have reasonable values and there are no signs of error. In the log file nothing shows up.
Working version
I had noticed that nCode usage depends on the type of hook, but I completely forgot to check the other parameters; as Hans noticed lParam doesn't point to a MSG for WH_CBT with nCode == HCBT_MOVESIZE; instead, lParam points to the new RECT and the HWND can be retrieved from wParam.
__declspec(dllexport)
LRESULT CALLBACK HookProcedure(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_MOVESIZE)
{
wchar_t text[1014];
GetWindowText((HWND)wParam, text, 1024);
if (wcscmp(text, L"Untitled - Notepad") == 0)
{
RECT *rect = (RECT *)lParam;
FILE *file;
fopen_s(&file, "C:\\Users\\Me\\hooklog.txt", "a+");
fprintf(file, "Move or size (%d, %d, %d, %d) for target window.\n", rect->left, rect->right, rect->top, rect->bottom);
fclose(file);
}
else
{
FILE *file;
fopen_s(&file, "C:\\Users\\Me\\hooklog.txt", "a+");
fprintf(file, "Move or size for some other window.\n");
fclose(file);
}
}
else
{
FILE *file;
fopen_s(&file, "C:\\Users\\Me\\hooklog.txt", "a+");
fprintf(file, "Something else.\n");
fclose(file);
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}