Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C#

Runtime Object Editor

Rate me:
Please Sign up or sign in to vote.
4.93/5 (136 votes)
30 May 20068 min read 300.8K   10.9K   307  
A powerful window/object editor to be used at runtime that allows viewing/changing of properties and fields, method invocations, and object hierarchy navigation.
/* ****************************************************************************
 *  RuntimeObjectEditor
 * 
 * Copyright (c) 2005 Corneliu I. Tusnea
 * 
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the author be held liable for any damages arising from 
 * the use of this software.
 * Permission to use, copy, modify, distribute and sell this software for any 
 * purpose is hereby granted without fee, provided that the above copyright 
 * notice appear in all copies and that both that copyright notice and this 
 * permission notice appear in supporting documentation.
 * 
 * Corneliu I. Tusnea (corneliutusnea@yahoo.com.au)
 * www.acorns.com.au
 * ****************************************************************************/


using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace RuntimeObjectEditor.Utils
{
	public delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);

	public enum GetWindowCmd : int
	{
		GW_HWNDFIRST = 0,
		GW_HWNDLAST,
		GW_HWNDNEXT,
		GW_HWNDPREV,
		GW_OWNER,
		GW_CHILD
	} ;

	public struct RECT
	{
		public int left;
		public int top;
		public int right;
		public int bottom;
	} ;

	public class NativeUtils
	{
		[DllImport("user32.dll")]
		public static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern IntPtr GetDesktopWindow();

		[DllImport("user32.dll")]
		public static extern bool EnumThreadWindows(int threadId, WindowEnumProc func, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern int GetWindowTextLength(IntPtr hwnd);

		[DllImport("user32.dll")]
		public static extern int GetWindowText(IntPtr hwnd, StringBuilder buffer, int bufferLen);

		[DllImport("user32.dll")]
		public static extern int GetClassName(IntPtr hwnd, StringBuilder buffer, int bufferLen);

		[DllImport("user32.dll")]
		public static extern bool IsWindowVisible(IntPtr hwnd);

		[DllImport("user32.dll")]
		public static extern bool SetForegroundWindow(IntPtr hwnd);

		[DllImport("user32.dll")]
		public static extern IntPtr GetParent(IntPtr hwnd);

		[DllImport("user32.dll")]
		public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int processID);

		[DllImport("user32.dll")]
		public static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

		[DllImport("user32.dll")]
		public static extern bool MoveWindow(IntPtr hwnd, int x, int y, int width, int height, bool repaint);

		[DllImport("user32.dll")]
		public static extern IntPtr GetWindow(IntPtr hwnd, int cmd);

		[DllImport("user32.dll")]
		public static extern IntPtr GetWindowRect(IntPtr hwnd, ref RECT rc);

		[DllImport("user32.dll")]
		public static extern IntPtr GetClientRect(IntPtr hwnd, ref RECT rc);

		public static string GetWindowText(IntPtr hwnd)
		{
			int bufLen = GetWindowTextLength(hwnd) + 1;

			StringBuilder buffer = new StringBuilder(bufLen);
			GetWindowText(hwnd, buffer, bufLen);

			return buffer.ToString();
		}

		public static string GetClassName(IntPtr hwnd)
		{
			StringBuilder buffer = new StringBuilder(256);
			GetClassName(hwnd, buffer, 256);

			return buffer.ToString();
		}


		public static bool IsTargetInDifferentProcess(IntPtr targetWindowHandle)
		{
			int targetProcessId;
			int thisProcessId;

			int targetThreadId = GetWindowThreadProcessId(targetWindowHandle, out targetProcessId);

			if (targetProcessId == -1)
			{
				return true;
			}

			thisProcessId = Process.GetCurrentProcess().Id;

			//NativeUtils.GetWindowThreadProcessId(this.Handle, out thisProcessId);

			return thisProcessId != targetProcessId;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead OneSaas - Cloud Integrations Made Easy
Australia Australia

Comments and Discussions