Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using PInvoke;
- namespace WF_Invoke
- {
- static class Program
- {
- delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
- [DllImport("user32.dll")]
- static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
- public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);
- [STAThread]
- static void Main()
- {
- // get process by exe name
- // var snapshot = Kernel32.CreateToolhelp32Snapshot(Kernel32.CreateToolhelp32SnapshotFlags.TH32CS_SNAPALL, 0);
- // var process = Kernel32.Process32First(snapshot);
- // while (process.HasValue)
- // {
- // if (string.Compare(process.Value.ExeFile, "notepad.exe") == 0) break;
- // process = Kernel32.Process32Next(snapshot);
- // }
- // snapshot.Dispose();
- //
- // var processId = process.Value.th32ProcessID;
- //
- var handles = new List<IntPtr>();
- foreach (ProcessThread thread in Process.GetProcesses().First(p => string.Compare(p.ProcessName, "notepad") == 0).Threads)
- EnumThreadWindows(thread.Id, (hWnd, lParam) =>
- {
- handles.Add(hWnd);
- return true;
- }, IntPtr.Zero);
- foreach (var handle in handles)
- {
- EnumChildWindows(handle, (hwnd, param) =>
- {
- User32.WINDOWINFO winInfo = User32.WINDOWINFO.Create();
- User32.GetWindowInfo(hwnd, ref winInfo);
- var className = User32.GetClassName(hwnd);
- if (className == "Edit" && winInfo.dwStyle == 0x50200104)
- {
- SendMessage(hwnd, (uint) User32.WindowMessage.WM_SETTEXT, IntPtr.Zero, "Hello");
- return false;
- }
- Trace.WriteLine(hwnd + ": " + className);
- return true;
- }, IntPtr.Zero);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement