Last active
March 21, 2021 00:46
-
-
Save NaxAlpha/8dd0ec425ffc05869353bc0cbd943a57 to your computer and use it in GitHub Desktop.
Windows GUI Hacking with C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
public class NativeControl { | |
public readonly IntPtr Handle; | |
public string Text { | |
get { | |
int length = GetWindowTextLength(Handle); | |
StringBuilder sb = new StringBuilder(length + 1); | |
GetWindowText(Handle, sb, sb.Capacity); | |
return sb.ToString(); | |
} | |
set { | |
SetWindowText(Handle, value); | |
} | |
} | |
private NativeControl(IntPtr hwnd) { | |
Handle = hwnd; | |
} | |
public static NativeControl FromHandle(IntPtr hwnd) { | |
return new NativeControl(hwnd); | |
} | |
// Native Imports | |
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] | |
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); | |
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] | |
static extern int GetWindowTextLength(IntPtr hWnd); | |
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] | |
public static extern bool SetWindowText(IntPtr hwnd, String lpString); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.Runtime.InteropServices; | |
class Program { | |
[DllImport("user32.dll")] | |
static extern IntPtr WindowFromPoint(Point p); | |
static void Main(string[] args) { | |
using(var p = Process.GetProcessesByName("notepad++")[0]) { | |
var hwnd = p.MainWindowHandle; | |
var x = NativeControl.FromHandle(hwnd); | |
x.Text = "Enjoy Hacking"; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment