views:

420

answers:

2

Hello everyone,

I have used the code supplied in the following CodeProject article in the past with success, but it only seems to partially work on Vista/7 (I'm guessing because of UAC). It works for the current thread, but it doesn't pick up system wide activity. I've tried adding requireAdministrator in the manifests and signing both the unmanaged and managed assemblies, and that doesn't appear to help.

Using Window Messages to Implement Global System Hooks in C#

My main goal is to use SetWindowsHookEx to catch the HSHELL_WINDOWCREATED and HSHELL_WINDOWDESTROYED messages in the unmanaged assembly, and notify the C# application so that it can use the information in real time. It basically just needs to know whenever a window is created or destroyed in the system.

I also found an example application that is pure C/C++ that works just fine in Vista/7, but I really don't want to have to write the entire application in C/C++ as I prefer C#.

System Wide Hook Example that Works

Anyone know how to fix the code in the first CodeProject article so that it works on Vista/7 as well? Or have any other examples of ways I can do this without creating timers and similar hacks in C#?

Thanks, Marc

+2  A: 

have a look at my program: http://wpfklip.codeplex.com. I've implemented system-wide shell hook so the program is able to catch window created, activated and so on. And it's written in c#

Trickster
I was unaware of the existence of RegisterShellHookWindow in User32... that makes things MUCH easier. Thank you for sharing this! I learned something new today after all. :)I'm going to play with it a bit tonight and if I can get it working, then I'll give you the answer credit. Thanks again.
Lusid
A: 

There's a key point here though:

1) Lusid is looking for a shell hook. These don't work in .NET, for the "DLL injection" reasons explained in the first article that Lusid linked to.

2) Trickster has written a keyboard hook. These work fine in C# (because they are hooked from some part of windows, rather than a specific application, and it can cope with injecting .NET).

a

AdamS