A Division of Technology Associates International Corporation
Maximo Blog

Interacting with other applications using C#

August 27, 2008 in C# by Michael Chrisman 2 Comments

It is possible to interact with another application. This include the ability to actually enter text in that application. The trick is getting the handle for the application. The commands we will use are part of the windows API. First we have to import the DLLs

   1:          [DllImport("User32.dll")]
   2:          public static extern Int32 SetForegroundWindow(int hWnd);
   3:          [DllImport("user32.dll")]
   4:          public static extern int FindWindow(string lpClassName, string lpWindowName);

Now we can use the FindWindow() method to get the handle for the window. FindWindow requires two parameters. The first is the name of the application and the second is the name of the window. However, you can just use one parameter by passing NULL in for the other. For example, you know that a special Fax printer driver dialog box has a window name of "Send Fax" but you don't know the actual application that is running, you could call passing in NULL and "Send Fax" respectively.

   1:  int handle = FindWindow(null, "Send Fax");

If the handle was not found, then handle will be set to '0.'

Even though we have a handle for the window, we need to make sure it has focus before we try interacting with it. To do this, we give that window focus using the SetForegroundWindow() method.

   1:  SetForegroundWindow(handle);

That that the window has focus, we can use the SendKeys object to send keystrokes to that window. SendKeys is part of the System.Windows.Form namespace.

   1:  using System.Windows.Forms;

The main methods you will use with SendKeys are Send() and SendWait(). Send just sends the keys while SendWait will wait for the keys to be processed.

   1:  SendKeys.SendWait("This is my text");

Of course you can use SendKeys to send special keys strokes. Here is a list of some of them (and yes you include the curly brackets.

Key stroke

What to pass in

Backspace {backspace}
Delete {delete}
Insert {insert}
End {end}
Home {home}
Tab {tab}
Escape {esc}
Enter {enter}
Help {help}
Break {break}
Caps Lock {capslock}
Num Lock {numlock}
Scroll Lock {scrolllock}
Page Up {pgup}
Page Down {pgdn}
Up Arrow {up}
Down Arrow {down}
Left Arrow {left}
Right Arrow {right}
Print Screen {ptrsc}
F1 {f1}
F2 {f2}
F3 {f3}
F4 {f4}
F5 {f5}
F6 {f6}
F7 {f7}
F8 {f8}
F9 {f9}
F10 {f10}
F11 {f11}
F12 {f12}
F13 {f13}
F14 {f14}
F15 {f15}
F16 {f16}
SHIFT +
CTRL ^
ALT %

So, let's put it all together.

   1:  using System;
   2:  using System.Windows.Forms;
   3:   
   4:  namespace demo
   5:  {
   6:      class InteractionDemo
   7:      {
   8:          [DllImport("User32.dll")]
   9:          public static extern Int32 SetForegroundWindow(int hWnd);
  10:          [DllImport("user32.dll")]
  11:          public static extern int FindWindow(string lpClassName, string lpWindowName);
  12:   
  13:          static void Main(string[] args)
  14:          {
  15:              int handle = FindWindow("notepad.exe", null);
  16:   
  17:              SetForegroundWindow(handle);
  18:              
  19:              SendKeys.SendWait("This is my text");
  20:              SendKeys.SendWait("^S"); // save the file
  21:          }
  22:      }
  23:  }

Comments

mihau
Posted on August 29, 2008

Hi,

Im also doing right now MEA things - starting now to look thru IFACETABLE handler - do you know how to use it? XML and FLAT file is really simple but on IFACE TABLE im stuck with jdbc driver for remote database connection - do you know how to setup this thing? Also I have tried to create iface tables on local db with isremote=0 but i cant find table/view in my local db (was doing MXPERSON_IFACE)..
Thanks for any help

Michal

Mike
Posted on August 29, 2008

Post a Comment

Remember my personal information.
Notify me of follow-up comments?

We don't know if you're a human. Confirm below: