Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SetWindowsHookEx vs. SetWinHookEventEx

Tags:

c#

.net

windows

api

What are the main differences (how it works/dependencies/purpose/minimal requirements) between SetWindowsHookEx and SetWinEventHook ?

I'm interested in intercepting events related to running windows applications from a Windows service using C#/.Net.

like image 527
JohnTube Avatar asked Feb 25 '14 20:02

JohnTube


2 Answers

SetWindowsHookEx sets low-level hooks which can intercept and re-write several system-wide events and messages (like capturing and re-writing keyboard input).

SetWinEventHook allows you to listen to Window events without having a window. It's higher level and less intrusive to the system.

Neither of this is required for running other applications from a service. Look at System.Diagnostics.Process (built into .Net) or RunProcess on NuGet

like image 89
Iain Ballard Avatar answered Nov 10 '22 11:11

Iain Ballard


If i understand you correctly, you want to use a service that you created to intercept the messages that are flowing between the OS and running Windows applications.

If that is the case your best choice is SetWindowsHookEx, this will create a system level hook which you will have to filter pass on to other appliations. You should note that this requires you to also build c++ .dll file from which you would call the SetWindowsHookEx function. Be very careful when coding because an error could lock up the entire system and you will need a restart to get things back to normal.

like image 3
Romaine Carter Avatar answered Nov 10 '22 11:11

Romaine Carter