Subscribe to Satan Eats Cheese Whiz        RSS Feed

Get/Set Window Text using the WINAPI and C#

Icon 1 Comments
Welcome to this entry on Getting / Setting window text on external applications using the Windows API, in C#. I have recently been playing with the WINAPI functions, and found them quite intriguing, and thought I'd share my experiences with you, as everyone likes a good hax every now and then just to satisfy curiosity. I apologise for this not being totally related to .NET as such, but this really does fill the non-existant bridge to setting window text on external applications; something .NET can't do afaik.

Getting and Setting window names are both relatively easy tasks, and are easy to grap, thankfully. Lets start by setting the window name of "Solitaire" to "Care Bear" (it kinda rhymes doesn't it?).

First we will need to create a declaration to the SetWindowText function in user32.dll. There are two parameters, the first being the window handle to the application, and the second being the text you'd like to replace the current text with.

[DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)]
public static extern bool SetWindowText(IntPtr hWnd, String strNewWindowName);

Here is how we can call the function to replace the text of our own Form.
SetWindowText(this.Handle, "Hello World");

This will access the Handle property of the form, and use it to replace the Window's text.

Now lets take a look at the FindWindow function. The FindWindow function will search through all the windows, and return the handle of the best suited window name you searched for, defined as a string parameter. We can alternatively search through the class names, but that won't be needed here. Lets take a look at how we can declare it.

[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]
public static extern IntPtr FindWindow(string className, string windowName);

The first parameter being the name of a class to search for the Window responsible for it, and the second parameter being the Window Name. Seeing as we don't want to use the class name, we will just give it a value of null. Now here is how we can set the window text of solitaire, using both the SetWindowText and FindWindow functions.

SetWindowText(FindWindow(null, "Solitaire"), "Care Bear");

Now lets have a look at using the GetWindowText function, to get the window text of the newly changed window name. But however, we have an issue. How are we supposed to keep track of the Window name it is supposed to be; the window text could be dynamic. Using Spy++ (a tool that comes with visual studio), I was able to find Solitaire has its own class named (who would have thought), "Solitaire". This is static, so we can be sure to know we are getting the correct window handle regardless of the window name getting changed. Hence we would be using the FindWindow function like so:

FindWindow("Solitaire", null)

Now we need to make our declaration of the GetWindowText function. This function will get the text of the window according to the handle and store it in a buffer.

[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Ansi)]
public static extern bool GetWindowText(IntPtr hWnd, [OutAttribute()] StringBuilder strNewWindowName,
	Int32 maxCharCount);

You might want to take note of the OutAttribute modifier, it is a member of the System.Runtime.InteropServices namespace, and is the kindof the equivilant of a pointer variable. Lets see how we can put this to use. First we will need a StringBuilder variable to store the buffer, then we will need to call the function. Here is an example:

StringBuilder sbWinText = new StringBuilder(256);
GetWindowText(FindWindow("Solitaire", null), sbWinText, 256);

Note the 256, this is the equivilant of one byte (0-255), and is ample amount of charactors for the window name. The first GetWindowText parameter is the handle, the second is the buffer to store the window name and the third is the maximum count of charactors the function will be using.

We can now get a string value of the window text by using the ToString() method, and for the purpose of this example we will use a MessageBox.


I hope you learnt something about getting and setting window text from the WINAPI, and thanks for viewing. :)

1 Comments On This Entry

Page 1 of 1

AnandVVN Icon

16 September 2008 - 04:06 AM
:^: Cool, I new to learning C#, the samples that I went through didn't actaully interest me, so I never got learning C#, but after trying these API functions in C#, now C# looks interesting to learn.


Page 1 of 1


Welcome to Satan Eats Cheese Whiz

Satan Eats Cheese Whiz is a programming blog, mostly directed at .NET Programming. Here you will find snippets, tutorials and other useful code that you can adapt or use with your projects. When using the code provided, please leave the comments intact, and link back to the page you got it from. Thanks for viewing!

Goto My Wiki
Syndicate this Blog (RSS)


0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)