using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace csharp_cg_bot_example { class Program { public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam); [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, String lpWindowName); [DllImport("user32", EntryPoint = "SendMessageW")] public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, String lParam); [DllImport("user32.dll")] public static extern int EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam); [DllImport("user32.dll")] static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); [DllImport("user32.dll")] static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); static IntPtr hwnd_send; static List room_list; public const int WM_SETTEXT = 0x000C; static bool init_room_list_cb(IntPtr hWnd, IntPtr lParam) { StringBuilder sb = new StringBuilder(256); GetClassName(hWnd, sb, sb.Capacity); if (sb.ToString() == "DlgGroupChat Window Class") { StringBuilder sbRoomName = new StringBuilder(256); GetWindowText(hWnd, sbRoomName, sbRoomName.Capacity); Console.WriteLine(sbRoomName.ToString()); room_list.Add(sbRoomName.ToString()); } return true; } static void init_room_list() { EnumWindowsProc childProc = new EnumWindowsProc(init_room_list_cb); EnumWindows(childProc, IntPtr.Zero); if (room_list.Count == 0) { Console.WriteLine("No rooms found"); } } static bool dlg_group_chat_cb(IntPtr hWnd, IntPtr lParam) { StringBuilder sb = new StringBuilder(256); GetClassName(hWnd, sb, sb.Capacity); if (sb.ToString() == "RichEdit20W") { hwnd_send = hWnd; } return true; } static void init_room_hwnd(string room_name) { IntPtr hwnd = FindWindow("DlgGroupChat Window Class", room_name); EnumWindowsProc childProc = new EnumWindowsProc(dlg_group_chat_cb); EnumChildWindows(hwnd, childProc, IntPtr.Zero); } static void send_text(string text) { SendMessage(hwnd_send, WM_SETTEXT, IntPtr.Zero, text); } static void Main(string[] args) { room_list = new List(); init_room_list(); Console.WriteLine("Rooms:" + room_list.Count); foreach (string room in room_list) { init_room_hwnd(room); send_text("Connected to the room: " + room); } } } }