Getting events of another application - c#

In my c# application I need to detect when a user clicks one of two buttons in a different, third party application.
I am able to get the Handle of the application but the MainWindowHandle returns 0.
I tried WndProc but for some reason the event will not fire in my application.
How can I get/intercept the button click event from that application into mine?

Global system hooks allow an application to intercept Windows messages intended for other applications. This has always been difficult to implement in C#. This project on Codeplex attempts to implement global system hooks by creating a DLL wrapper in C++ that posts messages to the hooking application's message queue. Put simply, this lets you implement any type of global Windows hook from managed code: http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx

Related

c# detect which messagebox (originated in another app) button has been pressed

Another application displays a messagebox (with a unique text inside it), user chooses Yes/No.
How to detect what he pressed in c#? (best in .Net up to 3.5). I could do polling with FindWindowEx (on another thread) but how to detect what button had been pressed? Also I don't think polling is the best way to do the job.
I need to know what the user has chosen in another app, so I can react accordingly in my own app. I don't have access to the other app's source code. Also to make it clear I don't want to click any of the buttons myself. I'm not afraid of a bit of c++, winapi and pinvoke
To monitor UI events in another application you can use UI Automation. To solve your specific problem you need to subscribe to a particular event (see Subscribing to UI Automation Events). To do so call IUIAutomation::AddAutomationEventHandler with a UIA_Invoke_InvokedEventId Event Identifier.
While UI Automation can be used to solve your problem, it is an assistive technology, mainly to enable accessibility needs and automated UI testing.
You could use either Anonymous or Named Pipes or WCF(Windows Communications Foundation).

Receive messages of other window

What I'm trying to do: receive messages of other process window (Spy++).
For example: I opened new notepad window: http://i.stack.imgur.com/vNg6h.png
And then I moved the mouse over the notepad window, the windows will send this message to the notepad window (or to the main window child window/s):
WM_MOUSEMOVE xPos=100,yPos=200
I want to receive this message exactly when then mouse event is happening (by event or while loop) (probably while loop with Application.doEvents() and Threading.Thread.Sleep(1)).
What I have tried:
create messages listener by the ManagedSpyLib (if you will import this library you can't compile your project... and if you will succeed to import this library it's not built for this mission)
I also searched about it and this is the most relevant result: http://www.codeproject.com/Articles/3923/InterSpy-An-integrated-Windows-message-trace-and-f
but this is a c++ project and I'm trying to do it in C# or VB.NET.
Good example is better than explanation for me. :)
The hooks that you need for this are WH_CALLWNDPROC and WH_GETMESSAGE. And these require DLL injection. You cannot inject managed code and so you will need to use unmanaged code for the hooking. You can then get your unmanaged injected DLL to communicate back to your C# application, but you will have to concede defeat on your attempts to write the hooking code in C#.
You need to look at the HOOK. That's how SPY++ works.
However, .Net is not very suitable for this, because you have to use a lot of WIN32 API functions.

Handle mouse event from another process

In my program I use class Process to start another application. This application starts fullscreen. My purpose is to handle mouse click from that application in my program. What WinApi functions should I use?
By "WinApi", I assume that you mean "Win32".
In order to handle messages of another process, you need to install a Win32 hook. See this article for more details. You need the WH_MOUSE hook and the SetWindowsHookEx Win32 API.
A hook function needs to be in a DLL, so that it can be injected in any process. You will need to filter the messages you get for the process that you started.
A hook function needs to be a global function so you must code it in C++. You will also need to use some inter-process communication to well, communicate with your main program, if needed.

Capture event for fingerprint reader in no active application

I have an application that validates users through a fingerprint reader. The validation is done in a method that i subscribed to manage the event, it looks like this.
FingerprintVerificationControl.OnComplete+=new DPFP.Gui.Verification.VerificationControl._OnComplete(FingerprintVerificationControl_OnComplete);
Everything goes well while i'm woriking with the application, i mean, when it has the focus, but, i have put it in the system tray using a notifyicon control and associating it with a contextmenu control to restore and close the app; so when it is in the system tray (is not the active application) i have no response from the fingerprint to manage the validation; the event of read the finger of the user does not fires.
My question is, what is the best way to manage that? Is it possible?. I found that i can do it if i make a windows service, other sites say that with Win32 API, others have examples but with keyboard events like presss key and so on. Any idea? any idea would be thank.
I have found that making a Windows Service is the best way to do stuff like that. However I don't any about the windows32 API cause I've been able to do everything I can without it.
As for the keyboard events, I have tried those and have found that they only work when the application has focus therefore, they are unusable. And services are just useful in general. You can try to talk to your application from your service via the local network so you don't have to rework the entire application.
I hope this is helpful.

Intercepting click event for all controls in an app in C# (WinForms)

I want to make an application to intercept all UI events in all the forms of my application and to write them to a log. This data can than be used to see which controls are the most used, in what order, etc. The problem is that I want this to happen automatically, without modifying the existing classes.
I made a prototype that attaches a method to a click event for all controls in a form, but how can this be done for all forms? Reflection needs a target object when manipulating events, but only the startup form can be easily accessed.
Is there a way to hook the constructor of an object? Then I could "inject" my method in all the events of the new form. Or maybe there is another way to do this.
Thanks in advance!
You can install a message filter.
A message filter is an object that implements IMessageFilter. WinForms calls your PreFilterMessage method for every message that passes through your thread's message loop. This is enough to monitor user input across the application (and gives you the option of manipulating it).
In Windows API this is done using local hooks (you can set local mouse hook using SetWindowsHookEx function). This is the proper way to do your task. In C# you need to use P/Invoke in order to get access to SetWindowsHookEx.
One more task would be to match the HWND (windows handle) to corresponding WinForms control.
Read this article for how to do this (via WM_GETCONTROLNAME message).
Also see this question which is a duplicate of yours.
You will have to work with the Win32's API Messages, I guess.
Here's a little example under the form of tutorial.
You should be able to achieve what you want with message filters - no direct P/Invoke to Win32-APIs required!
See the help on the IMessageFilter interface for more info.

Categories