- Windows Hook Example For Kids
- Windows Hook Example Free
- Windows Keyboard Hook Example C++
- Windows Hook Example For Sale
- Hook Examples For Essays
- Windows Svn Hook Examples
If I have a function foo()
that windows has implemented in kernel32.dll and it always returns true, can I have my program: 'bar.exe' hook/detour that Windows function and make it return false for all processes instead?
So, if my svchost, for example, calls foo()
, it will return false instead of true. The same action should be expected for all other processes currently running.
If so, how? I guess I'm looking for a system-wide hook or something.
AstroCBIn this example, WHCBT means events related to windows (creation,activation,destructions etc). The second parameter is the name of the Hook/Filter function that shall be called back by the OS – detailed below. Code Logic: 1. Install a thread or global hook. The hook procedure post a private message(WMKEYSTROKE or WMKEYINPUT) to the main Window(CppWindowsHookDlg). And WMKEYINPUT is sent when a real key is stroked if you setup a WHKEYBOARDLL hook. Log the information when main windows receive the private message. May 05, 2011 The code sample shows how to set up a Windows Hook to hook mouse and keyboard inputs in a VC# application Windows hook demo (CSWindowsHook) sample in C# for Visual Studio 2008 Breaking news from around the world Get the Bing + MSN extension. SetWindowsHookEx passes the module handle, a pointer to the hook-procedure entry point, and 0 for the thread identifier, indicating that the hook procedure should be associated with all threads in the same desktop as the calling thread. This sequence is shown in the following example. About this one, when using SetWindowsHookEx we don’t specify a target (victim) process. This function creates a global hook held by OS. Using the example, any key pressed on keyboard inside any application will trigger the hook function, right? So, this should be considered DLL Injection? For example, a filter function might want to receive all keyboard or mouse events. For Windows to call a filter function, the filter function must be installed — that is, attached to a Windows hook (for example, to a keyboard hook). Attaching one or more filter functions to a hook is known as setting a hook.
Clark GaebelClark Gaebel5 Answers

Take a look at Detours, it's perfect for this sort of stuff.
For system-wide hooking, read this article from MSDN.
First, create a DLL which handles hooking the functions. This example below hooks the socket send and receive functions.
Then, create a program to inject the DLL into the target application.
This should be more than enough to get you started!
xianxianDominate's all aformentioned techniques in simpleicty, flexability and functionality.
It was not discussed previously on Hook processes either. I've read all leaf's of this thread and with absolute certanty, EASYHOOK is vastly superiour. No matter if your using C, C++, CLR, whatever.
I'll paste a bit from the codeplex homepage, to ensure sufficient omage being paid.
The following is an incomplete list of features:
- A so called 'Thread Deadlock Barrier' will get rid of many core problems when hooking unknown APIs; this technology is unique to EasyHook
- You can write managed hook handlers for unmanaged APIs
- You can use all the convenience managed code provides, like NET Remoting, WPF and WCF for example
- A documented, pure unmanaged hooking API
- Support for 32- and 64-bit kernel mode hooking (also check out my PatchGuard 3 bypass driver which can be found in the release list)
- No resource or memory leaks are left in the target
- Experimental stealth injection mechanism that won't raise attention of any current AV Software
- EasyHook32.dll and EasyHook64.dll are pure unmanaged modules and can be used without any NET framework installed!
- All hooks are installed and automatically removed in a stable manner
- Support for Windows Vista SP1 x64 and Windows Server 2008 SP1 x64 by utilizing totally undocumented APIs, to still allow hooking into any terminal session.
- Managed/Unmanaged module stack trace inside a hook handler
- Get calling managed/unmanaged module inside a hook handler
- Create custom stack traces inside a hook handler
- You will be able to write injection libraries and host processes compiled for AnyCPU, which will allow you to inject your code into 32- and 64-Bit processes from 64- and 32-Bit processes by using the very same assembly in all cases.
- EasyHook supports RIP-relative addressing relocation for 64-Bit targets.
- No unpacking/installation necessary.
- The Visual Studio Redistributable is not required.
I'm happy that my hooker's still know a few tricks in comparison that makes me keep them around. But to be sure, when you need a HOOK, 99 times of 100, EASYHOOK'r will get you there faster. And it's quite actively maintained.

Please give more details of the function you want to hook! There are several ways to get your own code called in such a case, for instance:
You can build a fake DLL with the same name as the DLL that contains the function you want to hook (and copy it in the folder of
foo.exe
). This library would expose exactly the same functions as the original DLL. Each exposed function just bypasses the call to the original DLL, with the exception of the function you want to hook.You can change the function pointer table during run-time, for instance with the (commercial) Detour package that has been mentioned by 'kitchen'. However, doing such hooking can be done easily by your own, see this article to learn how.
You can find out where the specific function is called in
foo.exe
and just replace the assembly code that calls the function with a code that 'returnstrue
'. Basically, you're patching 'foo.exe
'..For specific functions, Windows offers automatic hooking, e.g. for keys and mouse events. Check the function SetWindowsHook for this.
This depends somewhat on the version of Windows you're wanting to target. Nonetheless, if you're playing on Pre-Vista, you can simply use SetWindowsHookEx to inject your DLL into every running process. Your DLL would then need to hook the appropriate function using Detours or similar.
mrduclawmrduclawIf you are writing your hook in assembly and not using Detours (for whatever reason), then you need some key information about returing FALSE:
- Win32, set EAX to 0
- Win64, set RAX to 0
You need to set EAX or RAX (depending upon platform) to zero as the last thing the function you are hooking does. That will result in the calling code receiving 0 as the return value (assuming they are returning an int or pointer type value).
Stephen KellettStephen Kellett