Automate another app

S

Steve

I have been programming in VB (classic) for many years and am quite
familiar with how I would accomplish this in that enviornment.
However I just started using C# and have no clue. For starters in VB
I would start the other application by calling the Shell function.
Then to actually control the other app I would first locate the window
handle for controls I wanted to interact with using Win32 APIs which
allow me to retrieve the hWnd for a window given its ControlID
(gathered from Spy++). I would then use the SendMessage API (with the
appropriate message constant) to enter text in edit fields, click
buttons, select items from a list, etc.

Can anyone here point me to some resources for figuring out how to do
these things in C#. Ideally I would like to minimize the use of Win32
APIs. IOW if there a framework tools available to do these task I
would rather use them.

Thanks in advance for your help,
Steve
 
P

Peter Duniho

Steve said:
I have been programming in VB (classic) for many years and am quite
familiar with how I would accomplish this in that enviornment.
However I just started using C# and have no clue. For starters in VB
I would start the other application by calling the Shell function.

See the System.Diagnostics.Process class.
Then to actually control the other app I would first locate the window
handle for controls I wanted to interact with using Win32 APIs which
allow me to retrieve the hWnd for a window given its ControlID
(gathered from Spy++).

See the Process.MainWindowHandle property.
I would then use the SendMessage API (with the
appropriate message constant) to enter text in edit fields, click
buttons, select items from a list, etc.

See the System.Windows.Forms.SendKeys class.

Note that you will have to bring the other process window to the
foreground if it doesn't happen automatically. You'll have to use the
unmanaged function SetForegroundWindow() for that. See
http://www.pinvoke.net/default.aspx/user32.SetForegroundWindow for
advice as to how to use that from C# (it requires p/invoke, which is a
form of interoperability in .NET).

Pete
 
S

Steve

See the System.Diagnostics.Process class.


See the Process.MainWindowHandle property.


See the System.Windows.Forms.SendKeys class.

Note that you will have to bring the other process window to the
foreground if it doesn't happen automatically.  You'll have to use the
unmanaged function SetForegroundWindow() for that.  Seehttp://www.pinvoke.net/default.aspx/user32.SetForegroundWindowfor
advice as to how to use that from C# (it requires p/invoke, which is a
form of interoperability in .NET).

Pete

I would rather not rely on SendKeys for just that reason (requires
target app to be foreground app).

In VB I also would have avoided Sendkeys for the same reasons and is
why (as described in my original post) I choose to do the work via the
Win32 API SendMessage. Doing this assures the target app will recieve
the messages as they are sent to the target windows message queue not
simply to the standard input queue (which goes to the foreground
window).

The reason for my questions was to see if there were any Framework
features which would provide the same functionality as the Win32 APIs
(ie. SendMessage, FindWindow, FindWindowEx, GetDlgItem, etc.) that I
would use in VB. If not then I will simply p/invoke the API in a
similar fashion as I am familiar with in VB.

It sounds like the only thing the Framework will offer me here is the
ability to get the hWnd of the other apps main window. That will be
usefull as it was not a straight forward thing using APIs.

Thanks,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top