rabbitears
asked on
strange hook problem
how come my keyboard hook is only being caught from my program even though it is a system wide hook? here's how i'm calling SetWindowsHookEx:
hHook = SetWindowsHookEx(WH_KEYBOA RD, (HOOKPROC)ShiftMonProc, (HINSTANCE)instance, NULL);
the rest of the relevant code looks like this:
BOOL AreModifiersOn(WORD wMod)
{
if(wMod & HOTKEYF_SHIFT)
if(!(GetKeyState(VK_SHIFT) & 0x80))
return FALSE;
if(wMod & HOTKEYF_CONTROL)
if(!(GetKeyState(VK_CONTRO L) & 0x80))
return FALSE;
if(wMod & HOTKEYF_ALT)
if(!(GetKeyState(VK_MENU) & 0x80))
return FALSE;
return TRUE;
}
LRESULT CALLBACK ShiftMonProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
if ((GetKeyState(hotkey) & 0x80) && AreModifiersOn(modkeys))
{
MessageBox(NULL, "oh", "yeah", MB_OK);
return TRUE;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
i could have sworn i had this code working earlier today but for some reason, it doesn't.. i'm running win '98, vc5, and MFC but the dll is just win32 api if it matters..
hHook = SetWindowsHookEx(WH_KEYBOA
the rest of the relevant code looks like this:
BOOL AreModifiersOn(WORD wMod)
{
if(wMod & HOTKEYF_SHIFT)
if(!(GetKeyState(VK_SHIFT)
return FALSE;
if(wMod & HOTKEYF_CONTROL)
if(!(GetKeyState(VK_CONTRO
return FALSE;
if(wMod & HOTKEYF_ALT)
if(!(GetKeyState(VK_MENU) & 0x80))
return FALSE;
return TRUE;
}
LRESULT CALLBACK ShiftMonProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
if ((GetKeyState(hotkey) & 0x80) && AreModifiersOn(modkeys))
{
MessageBox(NULL, "oh", "yeah", MB_OK);
return TRUE;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
i could have sworn i had this code working earlier today but for some reason, it doesn't.. i'm running win '98, vc5, and MFC but the dll is just win32 api if it matters..
A systemwide hook MUST reside in a DLL.
Here's an example:
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////
// Shared variables (must be initialized)
#pragma comment(linker, "-section:.shared,rws")
#pragma data_seg(".shared")
HHOOK g_hHook = NULL; // WH_GETMESSAGE hook handle
#pragma data_seg()
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////
// Global variables
HINSTANCE g_hinstDll = NULL; // Current DLL instance handle
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////
// DLL initialization and termination routine
BOOL APIENTRY DllMain(HINSTANCE hinstDll, DWORD reason, LPVOID reserved)
{
UNREFERENCED_PARAMETER(res erved);
if (reason == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls( hinstDll);
g_hinstDll = hinstDll; // Save DLL instance handle
break;
}
return TRUE; // Success
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////
// Set the hook
__declspec(dllexport) BOOL Hook(DWORD dwThreadId)
{
// Is a hook allready in place
if (g_hHook != NULL)
return FALSE;
g_hHook = SetWindowsHookEx(WH_CBT, CBTProc, g_hinstDll, dwThreadId);
return (g_hHook != NULL);
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////
// Remove the hook
__declspec(dllexport) BOOL Unhook(void)
{
BOOL rc;
rc = UnhookWindowsHookEx(g_hHoo k);
if (rc)
g_hHook = NULL;
return rc;
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////
// The hook procedure
__declspec(dllexport) LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM
lParam)
{
/* Do whatever needed here... */
// Pass through the hook chain
return CallNextHookEx(g_hHook, code, wParam, lParam);
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////
I highly recommend Richter's "advanced windows" book.
//////////////////////////
// Shared variables (must be initialized)
#pragma comment(linker, "-section:.shared,rws")
#pragma data_seg(".shared")
HHOOK g_hHook = NULL; // WH_GETMESSAGE hook handle
#pragma data_seg()
//////////////////////////
// Global variables
HINSTANCE g_hinstDll = NULL; // Current DLL instance handle
//////////////////////////
// DLL initialization and termination routine
BOOL APIENTRY DllMain(HINSTANCE hinstDll, DWORD reason, LPVOID reserved)
{
UNREFERENCED_PARAMETER(res
if (reason == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(
g_hinstDll = hinstDll; // Save DLL instance handle
break;
}
return TRUE; // Success
}
//////////////////////////
// Set the hook
__declspec(dllexport) BOOL Hook(DWORD dwThreadId)
{
// Is a hook allready in place
if (g_hHook != NULL)
return FALSE;
g_hHook = SetWindowsHookEx(WH_CBT, CBTProc, g_hinstDll, dwThreadId);
return (g_hHook != NULL);
}
//////////////////////////
// Remove the hook
__declspec(dllexport) BOOL Unhook(void)
{
BOOL rc;
rc = UnhookWindowsHookEx(g_hHoo
if (rc)
g_hHook = NULL;
return rc;
}
//////////////////////////
// The hook procedure
__declspec(dllexport) LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM
lParam)
{
/* Do whatever needed here... */
// Pass through the hook chain
return CallNextHookEx(g_hHook, code, wParam, lParam);
}
//////////////////////////
I highly recommend Richter's "advanced windows" book.
In case that wasn't clear: The example above is the DLL.
ASKER
my hook IS in a dll and in fact, it looks almost exactly like the example you posted..
the full source for the dll is as follows.. (minus the comments and such)
#define STRICT
#include <windows.h>
#include <commctrl.h>
#define SHIFTMON_DLL
#include "shiftmon.h"
HHOOK hHook = NULL;
HINSTANCE instance;
WORD hotkey;
WORD modkeys;
int proportion;
int moveapp;
void SetDll(HWND parent)
{
if (hHook == NULL)
{
hHook = SetWindowsHookEx(WH_KEYBOA RD, (HOOKPROC)ShiftMonProc, (HINSTANCE)instance, NULL);
}
else
{
if (hHook)
{
UnhookWindowsHookEx(hHook) ;
hHook = NULL;
}
}
}
void SetParams(WORD wHotKey, WORD wModKeys, int nProportion, int nMoveApp)
{
hotkey = wHotKey;
modkeys = wModKeys;
proportion = nProportion;
moveapp = nMoveApp;
}
BOOL AreModifiersOn(WORD wMod)
{
if(wMod & HOTKEYF_SHIFT)
if(!(GetKeyState(VK_SHIFT) & 0x80))
return FALSE;
if(wMod & HOTKEYF_CONTROL)
if(!(GetKeyState(VK_CONTRO L) & 0x80))
return FALSE;
if(wMod & HOTKEYF_ALT)
if(!(GetKeyState(VK_MENU) & 0x80))
return FALSE;
return TRUE;
}
LRESULT CALLBACK ShiftMonProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
if ((GetKeyState(hotkey) & 0x80) && AreModifiersOn(modkeys))
{
MessageBox(NULL, "oh", "yeah", MB_OK);
return TRUE;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
BOOL WINAPI DllMain(HANDLE hModule, ULONG dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
instance = (HINSTANCE)hModule;
return TRUE;
}
the full source for the dll is as follows.. (minus the comments and such)
#define STRICT
#include <windows.h>
#include <commctrl.h>
#define SHIFTMON_DLL
#include "shiftmon.h"
HHOOK hHook = NULL;
HINSTANCE instance;
WORD hotkey;
WORD modkeys;
int proportion;
int moveapp;
void SetDll(HWND parent)
{
if (hHook == NULL)
{
hHook = SetWindowsHookEx(WH_KEYBOA
}
else
{
if (hHook)
{
UnhookWindowsHookEx(hHook)
hHook = NULL;
}
}
}
void SetParams(WORD wHotKey, WORD wModKeys, int nProportion, int nMoveApp)
{
hotkey = wHotKey;
modkeys = wModKeys;
proportion = nProportion;
moveapp = nMoveApp;
}
BOOL AreModifiersOn(WORD wMod)
{
if(wMod & HOTKEYF_SHIFT)
if(!(GetKeyState(VK_SHIFT)
return FALSE;
if(wMod & HOTKEYF_CONTROL)
if(!(GetKeyState(VK_CONTRO
return FALSE;
if(wMod & HOTKEYF_ALT)
if(!(GetKeyState(VK_MENU) & 0x80))
return FALSE;
return TRUE;
}
LRESULT CALLBACK ShiftMonProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
if ((GetKeyState(hotkey) & 0x80) && AreModifiersOn(modkeys))
{
MessageBox(NULL, "oh", "yeah", MB_OK);
return TRUE;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
BOOL WINAPI DllMain(HANDLE hModule, ULONG dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
instance = (HINSTANCE)hModule;
return TRUE;
}
ASKER
errr, i just found out that the problem lies in the hook proc, if i comment out some of the code and just replace it with a messagebox, it works fine.. here's an example:
LRESULT CALLBACK ShiftMonProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
/*if ((GetKeyState(hotkey) & 0x80) && AreModifiersOn(modkeys))
{
MessageBox(NULL, "oh", "yeah", MB_OK);
return TRUE;
}
*/
MessageBox(NULL, "oh", "yeah", MB_OK);
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
of course this isn't a good idea :) and it doesn't get the job done, so what's wrong with the commented code?
LRESULT CALLBACK ShiftMonProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
/*if ((GetKeyState(hotkey) & 0x80) && AreModifiersOn(modkeys))
{
MessageBox(NULL, "oh", "yeah", MB_OK);
return TRUE;
}
*/
MessageBox(NULL, "oh", "yeah", MB_OK);
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
of course this isn't a good idea :) and it doesn't get the job done, so what's wrong with the commented code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What value has the variable hotkey?
And the autograder hits again! I sometimes wonder why I bother at all...