diff options
Diffstat (limited to 'hook')
-rw-r--r-- | hook/Hook.dsp | 126 | ||||
-rw-r--r-- | hook/hook.c | 96 | ||||
-rw-r--r-- | hook/hook.def | 6 | ||||
-rw-r--r-- | hook/hook.h | 39 |
4 files changed, 267 insertions, 0 deletions
diff --git a/hook/Hook.dsp b/hook/Hook.dsp new file mode 100644 index 0000000..6a4b773 --- /dev/null +++ b/hook/Hook.dsp @@ -0,0 +1,126 @@ +# Microsoft Developer Studio Project File - Name="Hook" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102 + +CFG=Hook - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "Hook.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "Hook.mak" CFG="Hook - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "Hook - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE "Hook - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +MTL=midl.exe +RSC=rc.exe + +!IF "$(CFG)" == "Hook - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "release" +# PROP Intermediate_Dir "release" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "HOOK_EXPORTS" /Yu"stdafx.h" /FD /c +# ADD CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "HOOK_EXPORTS" /Yu"stdafx.h" /FD /c +# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 +# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib /nologo /dll /machine:I386 /def:".\hook.def" /out:"../release/wpm.dll" /opt:nowin98 +# SUBTRACT LINK32 /pdb:none + +!ELSEIF "$(CFG)" == "Hook - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "HOOK_EXPORTS" /Yu"stdafx.h" /FD /GZ /c +# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "HOOK_EXPORTS" /Yu"stdafx.h" /FD /GZ /c +# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32 +# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"../debug/wpm.dll" /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "Hook - Win32 Release" +# Name "Hook - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\hook.c +# SUBTRACT CPP /YX /Yc /Yu +# End Source File +# Begin Source File + +SOURCE=.\hook.def + +!IF "$(CFG)" == "Hook - Win32 Release" + +# PROP Exclude_From_Build 1 + +!ELSEIF "$(CFG)" == "Hook - Win32 Debug" + +!ENDIF + +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\Hook.h +# End Source File +# End Group +# Begin Group "Resource Files" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" +# End Group +# End Target +# End Project diff --git a/hook/hook.c b/hook/hook.c new file mode 100644 index 0000000..0a8f456 --- /dev/null +++ b/hook/hook.c @@ -0,0 +1,96 @@ +// +// AUTHOR +// N. Nielsen +// +// LICENSE +// This software is in the public domain. +// +// The software is provided "as is", without warranty of any kind, +// express or implied, including but not limited to the warranties +// of merchantability, fitness for a particular purpose, and +// noninfringement. In no event shall the author(s) be liable for any +// claim, damages, or other liability, whether in an action of +// contract, tort, or otherwise, arising from, out of, or in connection +// with the software or the use or other dealings in the software. +// +// SUPPORT +// Send bug reports to: <nielsen@memberwebs.com> +// + +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +#include <windows.h> +#include "hook.h" + +#ifndef ASSERT + #include <assert.h> + #define ASSERT assert +#endif + +HMODULE g_hMod = NULL; + +#pragma data_seg(".shared") +HHOOK g_hHook = NULL; +LONG g_nCur = 0; +#pragma data_seg() + + +BOOL APIENTRY DllMain( HINSTANCE hModule, + DWORD ul_reason_for_call, + LPVOID lpReserved ) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + g_hMod = hModule; + break; + case DLL_PROCESS_DETACH: + g_hMod = NULL; + break; + } + return TRUE; +} + +LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam) +{ + ASSERT(g_hHook); + + if(code == HC_ACTION) + { + if(!(HIWORD(lParam) & KF_UP)) + { + // Although this is not perfect, it's good enough as the + // keyboard will usually only be used by one thread at a time + InterlockedExchange(&g_nCur, (LONG)(g_nCur + LOWORD(lParam))); + } + } + + return (int)CallNextHookEx(g_hHook, code, wParam, lParam); +} + +HOOK_API HRESULT InstallSpeedHook() +{ + ASSERT(g_hMod); + g_hHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hMod, 0); + + if(!g_hHook) + return HRESULT_FROM_WIN32(GetLastError()); + + return S_OK; +} + +HOOK_API HRESULT RemoveSpeedHook() +{ + ASSERT(g_hHook); + return UnhookWindowsHookEx(g_hHook) ? S_OK : HRESULT_FROM_WIN32(GetLastError()); +} + +HOOK_API int GetCurSpeedCount() +{ + return g_nCur; +} + +HOOK_API void ClearSpeedCount() +{ + InterlockedExchange(&g_nCur, 0L); +} + diff --git a/hook/hook.def b/hook/hook.def new file mode 100644 index 0000000..214cdbd --- /dev/null +++ b/hook/hook.def @@ -0,0 +1,6 @@ +; hook.def : declares the module parameters for the DLL.
+
+LIBRARY "wpm"
+
+SECTIONS
+ .shared READ WRITE SHARED
diff --git a/hook/hook.h b/hook/hook.h new file mode 100644 index 0000000..b2fd4e1 --- /dev/null +++ b/hook/hook.h @@ -0,0 +1,39 @@ +// +// AUTHOR +// N. Nielsen +// +// LICENSE +// This software is in the public domain. +// +// The software is provided "as is", without warranty of any kind, +// express or implied, including but not limited to the warranties +// of merchantability, fitness for a particular purpose, and +// noninfringement. In no event shall the author(s) be liable for any +// claim, damages, or other liability, whether in an action of +// contract, tort, or otherwise, arising from, out of, or in connection +// with the software or the use or other dealings in the software. +// +// SUPPORT +// Send bug reports to: <nielsen@memberwebs.com> +// + +#ifdef __cplusplus +#define EXTERNC extern "C" +#else +#define EXTERNC +#endif + +#ifdef HOOK_EXPORTS +#define HOOK_API EXTERNC __declspec(dllexport) +#else +#define HOOK_API EXTERNC __declspec(dllimport) +#endif + +HOOK_API HRESULT InstallSpeedHook(); +HOOK_API HRESULT RemoveSpeedHook(); + +HOOK_API int GetCurSpeedCount(); +HOOK_API void ClearSpeedCount(); + + + |