Skip to content

Gh61/csharp-global-windows-hook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C# Global Windows Hook

Purpose of this repository is to show how to hook (using WINAPI method SetWindowsHookEx) to Global Windows Event with C#.

Microsoft says only supported events are WH_KEYBOARD_LL and WH_MOUSE_LL wich is only partialy true.

Small backstory

Because I needed to write application wich block keypresses only from certain keyboard I need cooperation of Raw Input API (check for concrete input device) and Keyboard HOOK (block keys). I have done some pieces togethter and has working Raw Input and Keyboard Low-Level hook. This seems OK, but later I found that this Low-Level event is called before RawInput and I need WH_KEYBOARD event, called after RawInput. So I did a try and it worked*:

* I only thought it worked, it was another events firing to my callback.

Not working (yet?)

Sorry for disappointing you. I thought it was working but it wasn't. There is some chance someone could take this to the end.

Registering hook is not causing any error. (In LastWinError is success code). Maybe there is something that must be done or this is really not possible.

There are some alternatives for this problem:

  • write part or whole application in C++ (if only part, it may be used by pinvoke from C# app)Start here
  • use of custom driver wich does this work for you.

Idea

In linked article they said:

To install a global hook, a hook must have a native DLL export to inject itself in another process that requires a valid, consistent function to call into. This behavior requires a DLL export.

I did a small search and found this nuget: Unmanaged Exports (DllExport for .Net).

They said it isn't working because C# doesn't have DLL Export, but here it is (After compilation the task from this package does the (Magic) job. It takes the IL and add required exports of methods decorated with attribute [DllExport]).

Important parts of code

TODO

Event types

I am using this solution only with event type WH_KEYBOARD. But I assume it will work with other event types (with changes of lParam structure) too.

If someone tested new event type, please let me know, so i will update this table of supported event types:

Event Type Value State
WH_CALLWNDPROC 4 ?
WH_CALLWNDPROCRET 12 ?
WH_CBT 5 ?
WH_DEBUG 9 ?
WH_FOREGROUNDIDLE 11 ?
WH_GETMESSAGE 3 ?
WH_JOURNALPLAYBACK 1 ?
WH_JOURNALRECORD 0 ?
WH_KEYBOARD 2 🗙
WH_KEYBOARD_LL 13 native support
WH_MOUSE 7 ?
WH_MOUSE_LL 14 native support
WH_MSGFILTER -1 ?
WH_SHELL 10 ?
WH_SYSMSGFILTER 6 ?

About

C# Global Windows Hook

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages