improve.dk
Just another mindless drone looking for the perfect stack
posts - 227, comments - 489

Set text by handle

Written on April 4, 2007 by Mark S. Rasmussen in Development: .NET, Development: Win32

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:

win32_3_2

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

win32_3_1

Feedback

No comments posted yet.

Post Comment

Name  
Email
Url
Comment
Please add 4 and 7 and type the answer here: