Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- namespace SendMessage
- {
- using System.IO.MemoryMappedFiles;
- using System.Runtime.InteropServices;
- class Program
- {
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- internal static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- internal static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- internal static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- internal static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
- private const int WM_USER = 0x400;
- private const int WM_MCOMMAND = WM_USER + 200;
- // Define EnumWindowsProc outside of Program class and make it public
- public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
- static void Main(string[] args)
- {
- string executableName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
- if (args.Length == 0 || !args[0].StartsWith("/"))
- {
- Console.WriteLine("Usage: {0} <command>", executableName);
- Console.WriteLine("Example: {0} /echo -s Hello World", executableName);
- return;
- }
- // Enumerate through all windows to find AdiIRC
- IntPtr adiircHwnd = IntPtr.Zero;
- EnumWindows((hWnd, lParam) =>
- {
- StringBuilder className = new StringBuilder(256);
- GetClassName(hWnd, className, className.Capacity);
- if (className.ToString().Contains("WindowsForms10.Window.8.app.0.282af8c_r6_ad1")) // Adjust the condition as per the actual window class name
- {
- adiircHwnd = hWnd;
- return false; // Stop enumeration
- }
- return true; // Continue enumeration
- }, IntPtr.Zero);
- if (adiircHwnd == IntPtr.Zero)
- {
- Console.WriteLine("Command not sent. Window not found.");
- return;
- }
- // Join all arguments into a single string
- string arguments = string.Join(" ", args);
- // Send message to AdiIRC window
- using (var mappedFile = MemoryMappedFile.CreateNew("mIRC1", 8192))
- {
- using (Stream stream = mappedFile.CreateViewStream())
- {
- byte[] bytes = Encoding.UTF8.GetBytes(arguments);
- stream.Write(bytes, 0, bytes.Length);
- }
- SendMessage(adiircHwnd, WM_MCOMMAND, 1, 1);
- }
- Console.WriteLine("Command sent successfully.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement