Mark S. Rasmussen improve.dk
Apr 04
2007

This time we won’t be reading the text from a window, we’ll be setting it.

Like last time, open an Internet Explorer browser and obtain a handle to the address field.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Globalization;

namespace Set_text_by_handle
{
	class Program
	{
		// A Win32 constant
		const int WM_SETTEXT = 0x000C;

		// An overload of the SendMessage function, this time taking in a string as the lParam.
		[DllImport("User32.dll")]
		public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, string lParam);

		static void Main(string[] args)
		{
			// First, read the handle from the console, remember this has to be in HEX format!
			int handle = int.Parse(Console.ReadLine(), NumberStyles.HexNumber);

			// Now we'll send the WM_SETTEXT message to the window, passing the text
			// through the lParam parameter.
			SendMessage(handle, WM_SETTEXT, 0, "http://www.improve.dk");

			// And we're done
			Console.Write("Text set!");
			Console.Read();
		}
	}
}

And the result:

Note that we have not navigated to the address, we have only set it!

Mark S. Rasmussen
I'm the CTO at iPaper where I cuddle with databases, mold code and maintain the overall technical & team responsibility. I'm an avid speaker at user groups & conferences. I love life, motorcycles, photography and all things technical. Say hi on Twitter, write me an email or look me up on LinkedIn.