Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interact with another program with C#

I wish to interact with other Windows applications from my C# application. This should be able to be done in the background, while a user continues doing something else. The target applications are not mine, but I do know what they are, so their UI/etc. is known.

I am basically trying to automate the process of various tasks across a variety of programs.

So far, the only solution I can think of, is having the code switch to the relevant window, and carry out the task with the mouse. (Controlling the mouse with methods as shown here)

The downfall of this approach of course, is it cannot run in the background and stops the user from carrying out other tasks.

Of course if these target applications received 'fake' mouse clicks, while still running in the background, this solution would be great. So if that's possible, please do share! Otherwise, any solution for such a problem would be brilliant.

like image 898
Joel Avatar asked Mar 04 '13 16:03

Joel


1 Answers

I just want to say that what you are doing is going to be pretty tough to make reliable without cooperation from the developers of the other applications. However, you can use Windows Messages to accomplish what you're trying to do. This is a way to send information to a window without explicit communications being set up. You'll need to use native methods to do this, but it's pretty straightforward. Be careful, however, as implementing this is not as trivial as it looks - you can get into deadlocks if you accidentally send a message to a closed (or invisible) window, for example.

This is too big a topic for a Stack Overflow post, but here are some relevant functions to get you started:

PostMessage and SendMessage are the main functions you can use to send messages to other applications.

The answers to this post have some basic examples of how to use these functions.

Finally, here is a list of Windows message codes for reference.

like image 94
Steve Westbrook Avatar answered Sep 28 '22 11:09

Steve Westbrook