7

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.

pnuts
  • 58,317
  • 11
  • 87
  • 139
JohnTube
  • 1,782
  • 27
  • 46

2 Answers2

8

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

Iain Ballard
  • 4,433
  • 34
  • 39
3

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.

Romaine Carter
  • 637
  • 11
  • 23
  • 1
    Another constraint is that since Vista it's not possible to directly set a global hook from a Windows service as mentioned [here](http://stackoverflow.com/questions/5815424/global-keyboard-hook-from-windows-service) and a solution is to spawn a child process to do that instead. – JohnTube Feb 25 '14 at 21:20