c# - How to give input to a windows application programmatically_ - Stack Overflow
c# - How to give input to a windows application programmatically_ - Stack Overflow
- Stack Overflow
I found the answer to the above question very useful. In the same way, can I do it for windows
applications? Like can I give input into a Windows form (like .net, C# applications) application's
text box programmatically? Can I do the action of button click from a program?
If so, can I do it from a C program (with Tiny C compiler or any other on windows platform)? If
not, how and in which language can I do it? Please don't take it as a silly question. Any
example codes will be very much appreciated.
Edit: Can I do the same with web applications and web pages?
c# c automation
Share Improve this question edited May 23, 2017 at 10:31 asked Jun 4, 2009 at 12:33
Follow Community Bot Enjoy coding
1 1 4,238 12 39 49
Exactly what are you trying to accomplish? Is this for testing, or do you simply need to invoke the
functionality of the windows forms application from another program? If the latter, then you could host
a WCF service in the Windows Forms application and call it from other programs. Same with a web
application. – John Saunders Jun 4, 2009 at 13:48
Sorted by:
4 Answers
Highest score (default)
If your application mainly just needs to manipulate other windows, by sending keystrokes etc.
i would use AutoIt. It's mainly function is exactly this job and it does it very well.
4
So maybe you should give it a try, cause not every problem is a nail and can be solved with a
hammer (C#) ;-))
1 Autoit is an adhoc programming language which seemed to me like a magical language at first. Then I
fell in love with it and started doing crazy things with it. Thanks for the answer on achieving the popular
https://stackoverflow.com/questions/950265/how-to-give-input-to-a-windows-application-programmatically 1/5
3/16/23, 5:11 PM c# - How to give input to a windows application programmatically? - Stack Overflow
@Enjoy coding: Yes i wished just the half of them had upvote my answer. ;-)) – Oliver May 13, 2011 at
11:11
FYI: Winspector is a very interesting tool which makes heavy use of this as well (also to debug
or otherwise first inspect the windows you're trying to programmatically access).
Share Improve this answer Follow edited Jun 4, 2009 at 13:07 answered Jun 4, 2009 at 12:39
fretje
8,302 2 49 61
Isn't it mad that applications can be interfered with like that? Is there any sort of process-level
protection in Windows, or can every process see every process? – dreamlax Jun 4, 2009 at 12:51
Not that I know of no... That's e.g. how Revelation (snadboy.com) works to reveal passwords in
password fields... Although there are other measures in place to prevent that now... – fretje Jun 4, 2009
at 12:54
It's not mad at all to allow windows to be controlled remotely. Without an API to allow this you couldn't
have things like debuggers, task managers, automated testing tools, and innumerably useful other
applications. Heck, without the ability to send windows messages to any window you couldn't even shut
down unresponsive apps. – Ron Warholic Jun 4, 2009 at 14:04
@BlodBath, you obviously do not know what I mean. If the application was unresponsive, why would it
respond to a message that you sent it? Killing a process is done entirely independently from messaging.
Also, debugging is done by spawning a sub-process, or by attaching to a process, not by messaging
windows. Not all processes have window procedures yet these can still be debugged and killed.
– dreamlax Jun 5, 2009 at 13:08
2 It's much more challenging for windows apps. Because individual controls also count as
windows, the total window count will be much higher than you might expect; and finding the
specific control you want to send input to can take a lot of work.
https://stackoverflow.com/questions/950265/how-to-give-input-to-a-windows-application-programmatically 2/5
3/16/23, 5:11 PM c# - How to give input to a windows application programmatically? - Stack Overflow
But you can programmatically move, resize, check, fill, maximize or otherwise affect an app's
windows, once you know which one is your target.
I wrote code to do the discovery process a few years ago. It found all the windows of a target
app, then using their size & location data, produced a translucent overlay of each child
window, along with the handle number. So I could visually tell which control went with which
handle.
EDIT: Added some code. This is some basic C# interop code that will let you make easy calls
into user32.dll, which holds the fns to which fretje referred. This just gives you the basic calls
for discovery and manipulation; you'll still have to do the hard work of enumerating and
examining what you find. If you can find a 3rd-party package that does the job for you, save
yourself the trouble; I only did it as a learning experience, and it was pretty laborious.
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
namespace WinAPI
{
[Flags] public enum WindowStyleFlags : uint
{
WS_OVERLAPPED = 0x00000000,
WS_POPUP = 0x80000000,
// more...
}
https://stackoverflow.com/questions/950265/how-to-give-input-to-a-windows-application-programmatically 3/5
3/16/23, 5:11 PM c# - How to give input to a windows application programmatically? - Stack Overflow
[DllImport("user32")]
public extern static int BringWindowToTop (IntPtr hWnd);
[DllImport("user32")]
public extern static int EnumWindows( EnumWindowsCallback lpEnumFunc, int
lParam );
[DllImport("user32")]
public extern static int EnumChildWindows( IntPtr hWndParent,
EnumWindowsCallback lpEnumFunc, int lParam );
[DllImport( "user32.dll" )]
public static extern int EnumThreadWindows( IntPtr hWndParent,
EnumWindowsCallback callback, int lParam );
[DllImport( "user32.dll" )]
public static extern int FindWindow( string lpClassName, string WindowName );
[DllImport( "user32.dll" )]
public static extern int FindWindowEx( IntPtr hWnd, IntPtr hWnd2, string lpsz,
string lpsz2 );
[DllImport("user32")]
public extern static int FlashWindow ( IntPtr hWnd, ref FLASHWINFO pwfi);
[DllImport("user32")]
public extern static IntPtr GetAncestor( IntPtr hWnd, uint gaFlags );
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int GetClassName ( IntPtr hWnd, StringBuilder lpClassName,
int nMaxCount);
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static uint GetWindowLong( IntPtr hwnd, int nIndex);
[DllImport("user32")]
public extern static int GetClientRect( IntPtr hWnd, ref RECT lpRect);
[DllImport("user32")]
public extern static int GetWindowRect( IntPtr hWnd, ref RECT lpRect);
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int GetWindowText( IntPtr hWnd, StringBuilder lpString,
int cch );
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int GetWindowTextLength( IntPtr hWnd );
[DllImport("user32")]
https://stackoverflow.com/questions/950265/how-to-give-input-to-a-windows-application-programmatically 4/5
3/16/23, 5:11 PM c# - How to give input to a windows application programmatically? - Stack Overflow
public extern static int IsIconic(IntPtr hWnd);
[DllImport("user32")]
public extern static int IsWindowVisible( IntPtr hWnd );
[DllImport("user32")]
public extern static int IsZoomed(IntPtr hwnd);
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int PostMessage( IntPtr hWnd, int wMsg, int wParam, int
lParam);
[DllImport( "user32.dll" )]
public static extern int RealGetWindowClass( IntPtr hWnd, StringBuilder
pszType, uint bufferSize );
[DllImport("user32")]
public extern static int ScreenToClient( IntPtr hWnd, ref POINT lpPoint);
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int SendMessage( IntPtr hWnd, int wMsg, IntPtr wParam,
IntPtr lParam);
[DllImport("user32.dll")]
public extern static int SetForegroundWindow (IntPtr hWnd);
[DllImport( "user32.dll" )]
public static extern int SetWindowText( IntPtr hWnd, string lpsz );
}
}
Share Improve this answer Follow edited Jun 4, 2009 at 13:44 answered Jun 4, 2009 at 12:54
user447688
Check out the following SO question. There are numerous vendor and open-source tools to
do this for you, so you don't have to code at to the Win32 API.
0
automated-testing-of-windows-forms
Share Improve this answer Follow edited May 23, 2017 at 11:54 answered Jun 4, 2009 at 13:55
Community Bot Tom E
1 1 2,519 2 17 22
https://stackoverflow.com/questions/950265/how-to-give-input-to-a-windows-application-programmatically 5/5