0

I'm trying to get messages from window handles the way a software like Microsoft Spy++ does. I am pretty sure there should be a solution using p/invoke, but I have been unable to figure out what methods will get me the solution.

Here's a screenshot of Microsoft Spy++ getting messages from another window handle. Microsoft Spy ScreenShot

Any piece of advice or information would definitely be helpful. Thanks! :)

Prince Owen
  • 1,225
  • 12
  • 20
  • Are you also writing a debugging style tool? I can think of some nasties you can do if you can effectively debug the target process but you shouldn't be trying to do that if you're just trying to interact "normally" with other applications. Having intercepted the information, what do you plan to do with it? – Damien_The_Unbeliever Sep 10 '18 at 14:52
  • [SetWindowsHookExA function](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowshookexa) – γηράσκω δ' αεί πολλά διδασκόμε Sep 10 '18 at 14:54
  • 2
    SetWindowHookEx() does this, however the code must be hosted in a DLL that Windows loads into the target process - this is not achievable in a .Net language. – Alex K. Sep 10 '18 at 14:54
  • 2
    `SetWindowsHookEx` is the name of the magic, specifically one of type `WH_CALLWNDPROC`. Doing this from managed code is neither simple nor convenient, because you need (at the least) a DLL exporting a suitable unmanaged hook entry point, which you can't create in pure C#, not even with unsafe code. There are other ways of spying on applications that may be more appropriate or more convenient, depending on *why*, exactly, you're trying to get these messages (for example, the UI automation APIs). – Jeroen Mostert Sep 10 '18 at 14:55
  • @Damien_The_Unbeliever Well, nothing complex, I'm mainly interest in keyboard events actually. I am trying to get feedback during an automation. – Prince Owen Sep 10 '18 at 15:03
  • @AlexK. Wow, So there's absolutely no way I can achieve something similar? Even if it is just for keyboard events... – Prince Owen Sep 10 '18 at 15:07
  • 1
    Keyboard hooks specifically (not generic message hooks) *can* be written in managed code, and there are many questions on SO dealing with that. [For example](https://stackoverflow.com/q/604410/4137916). If you're interested in high-level changes to an application's state, there's also the [UI automation APIs](https://learn.microsoft.com/dotnet/framework/ui-automation/ui-automation-events-overview) -- they don't capture keyboard events, but rather the results of them, which may or may not be more convenient. – Jeroen Mostert Sep 10 '18 at 15:10
  • @JeroenMostert Well, I will give them both a try, thanks a lot! – Prince Owen Sep 10 '18 at 15:20

0 Answers0