C# Send Text to Window

This tutorial focuses on sending text from a C# program to any other window by using 2 functions provided by user32.dll. The big advantage of this method is that the window you’re sending the text to doesn’t require focus.
Don’t forget to include those namespaces:


1. FindWindowEx

This method gets all the child elements from a parent element: for example it can get the handle of a textbox(child) from the window(parent).
In C#:

2. SendMessage

This one simply sends a message to the specified handle (it might be a window, a textbox, anything…).
We’ll use this to send the data we want.
In C#:

How it works

In this tutorial I’ll send some text to Notepad – it’s just an example, but this method works for any program. Basically you get the window’s handle from the process and then, by using FindWindowEx you get the children’s handle (that’s the textbox). Finally, you send the text to that child.
* you need to know the child element’s name – you can find it easy by using Spy++.
I’ll post below a C# application that changes the text from notepad‘s window.

Using this method, you won’t need to actually give focus to the window – but you’ll have to know some additional information about the program’s structure – that’s because you need to know what child to select, where is that child located, etc..
But as I said before, you can find this out by using Spy++ (from Visual Studio).
Previous
Next Post »