tags:

views:

423

answers:

2

I know something about MACROS. I don't mean the ASSEMBLY language kind. I am talking about those programs that you can use perform repetitions actions on another program. I am talking about those programs that you can use to record a series of events on your computer, like, mouse movements and button clicks and then you can play them back. Some of them are elaborate enough to run only on a paricular app that you designate.

I wrote one of sorts once. It was a program that launched an Excel sessions and then used the dynamic data exchage pipe of some kind to feed the excell session script commands. It worked.

But something on the level of the operating system, I imagine, is a whole different story.

How does someone go about writing a "macro" in C#?

I think the approach I will take is to use the spy routine that comes with the development environment to get a list of the proper messages and parameters (wm_lbuttondown for example) and then use dynamic data exchange to send those messages to the app.

So I have three questions.

  1. Is this the best way to do this?
  2. How do I get a handle to an app that is already running?
  3. How do I send user-like messages to an app that is already running?
+1  A: 

There are different answers based on many following factors:

  • is it 3rd party or your own application?
  • does it have automation interface
  • GUI toolkit used in app

If it is a 3rd party app then you need to work on Windows API level via PInvoke - subclassing WinMain proc, capturing and sending input messages, etc. There are 3rd party library for that task. C# obviously is not a right choice for such task.

In case application has automation model (like Excel) it's a pretty straight forward to write program that will be interact with this app.

If it's your own application you want to enhance with macros functionality then you should take this into account on design state. If you use Command pattern from the beginning then it's not hard to program macro recording.

You should provide more details to get a better answer.

Oh, I almost forgot to answer those three questions

  1. Is this the best way to do this?

Depends on concrete scenario

  1. How do I get a handle to an app that is already running?

Depends on application. If it's a native Win app you can easily get process Id and window's handle via WinApi.

  1. How do I send user-like messages to an app that is already running?

Once again it depends on application type. For native win apps you can easily send WM_XXX messages via WinAPI

aku
It is a 3rd party app.It does not have an automation interface.This is mostly a C# question. There are system commands you can use to grab ahold of a handle of an already running application and there are ways of using this handle to send commands to to simulate a user interface.
xarzu
A: 

Unless its something you need to add in your own program you can just download a keyboard/mouse macro program and use it to perform these repeatable actions.

On the other hand to perform macro's in your own program you would want to find a way to record the buttons clicked and write them to a temporary list that can be saved and then run the list by clicking the buttons (programmically).

babor_7uiu