Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using PInvoke;
  9.  
  10. namespace WF_Invoke
  11. {
  12. static class Program
  13. {
  14. delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
  15.  
  16. [DllImport("user32.dll")]
  17. static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
  18.  
  19. public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
  20.  
  21. [DllImport("user32.dll")]
  22. [return: MarshalAs(UnmanagedType.Bool)]
  23. public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
  24.  
  25. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  26. static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);
  27.  
  28. [STAThread]
  29. static void Main()
  30. {
  31. // get process by exe name
  32. // var snapshot = Kernel32.CreateToolhelp32Snapshot(Kernel32.CreateToolhelp32SnapshotFlags.TH32CS_SNAPALL, 0);
  33. // var process = Kernel32.Process32First(snapshot);
  34. // while (process.HasValue)
  35. // {
  36. // if (string.Compare(process.Value.ExeFile, "notepad.exe") == 0) break;
  37. // process = Kernel32.Process32Next(snapshot);
  38. // }
  39. // snapshot.Dispose();
  40. //
  41. // var processId = process.Value.th32ProcessID;
  42. //
  43. var handles = new List<IntPtr>();
  44. foreach (ProcessThread thread in Process.GetProcesses().First(p => string.Compare(p.ProcessName, "notepad") == 0).Threads)
  45. EnumThreadWindows(thread.Id, (hWnd, lParam) =>
  46. {
  47. handles.Add(hWnd);
  48. return true;
  49. }, IntPtr.Zero);
  50.  
  51. foreach (var handle in handles)
  52. {
  53. EnumChildWindows(handle, (hwnd, param) =>
  54. {
  55. User32.WINDOWINFO winInfo = User32.WINDOWINFO.Create();
  56. User32.GetWindowInfo(hwnd, ref winInfo);
  57. var className = User32.GetClassName(hwnd);
  58. if (className == "Edit" && winInfo.dwStyle == 0x50200104)
  59. {
  60. SendMessage(hwnd, (uint) User32.WindowMessage.WM_SETTEXT, IntPtr.Zero, "Hello");
  61. return false;
  62. }
  63.  
  64. Trace.WriteLine(hwnd + ": " + className);
  65. return true;
  66. }, IntPtr.Zero);
  67. }
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement