Advertisement
Guest User

Untitled

a guest
Apr 14th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | Source Code | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5.  
  6. namespace SendMessage
  7. {
  8. using System.IO.MemoryMappedFiles;
  9. using System.Runtime.InteropServices;
  10.  
  11. class Program
  12. {
  13. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  14. internal static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
  15.  
  16. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  17. internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  18.  
  19. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  20. internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  21.  
  22. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  23. internal static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
  24.  
  25. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  26. internal static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
  27.  
  28. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  29. internal static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
  30.  
  31. private const int WM_USER = 0x400;
  32. private const int WM_MCOMMAND = WM_USER + 200;
  33.  
  34. // Define EnumWindowsProc outside of Program class and make it public
  35. public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
  36.  
  37. static void Main(string[] args)
  38. {
  39. string executableName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
  40.  
  41. if (args.Length == 0 || !args[0].StartsWith("/"))
  42. {
  43. Console.WriteLine("Usage: {0} <command>", executableName);
  44. Console.WriteLine("Example: {0} /echo -s Hello World", executableName);
  45. return;
  46. }
  47.  
  48. // Enumerate through all windows to find AdiIRC
  49. IntPtr adiircHwnd = IntPtr.Zero;
  50. EnumWindows((hWnd, lParam) =>
  51. {
  52. StringBuilder className = new StringBuilder(256);
  53. GetClassName(hWnd, className, className.Capacity);
  54.  
  55. if (className.ToString().Contains("WindowsForms10.Window.8.app.0.282af8c_r6_ad1")) // Adjust the condition as per the actual window class name
  56. {
  57. adiircHwnd = hWnd;
  58. return false; // Stop enumeration
  59. }
  60.  
  61. return true; // Continue enumeration
  62. }, IntPtr.Zero);
  63.  
  64. if (adiircHwnd == IntPtr.Zero)
  65. {
  66. Console.WriteLine("Command not sent. Window not found.");
  67. return;
  68. }
  69.  
  70. // Join all arguments into a single string
  71. string arguments = string.Join(" ", args);
  72.  
  73. // Send message to AdiIRC window
  74. using (var mappedFile = MemoryMappedFile.CreateNew("mIRC1", 8192))
  75. {
  76. using (Stream stream = mappedFile.CreateViewStream())
  77. {
  78. byte[] bytes = Encoding.UTF8.GetBytes(arguments);
  79. stream.Write(bytes, 0, bytes.Length);
  80. }
  81.  
  82. SendMessage(adiircHwnd, WM_MCOMMAND, 1, 1);
  83. }
  84.  
  85. Console.WriteLine("Command sent successfully.");
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement