As for the window descriptor (that is, the number), to display its title, I can’t get it, can someone tell me?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; namespace parol_udal { class Program { [DllImport("user32.dll",CharSet=CharSet.Auto,SetLastError=true)] public static extern IntPtr FindWindow(string sClassName,string sWindowName); [DllImport("user32.dll",CharSet=CharSet.Auto,SetLastError=true)] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName); [DllImport("user32.dll",CharSet=CharSet.Auto ,SetLastError = true)] public static extern int GetWindowText (IntPtr hwnd,StringBuilder lpString,int nMaxCount); [DllImport("user32.dll",CharSet=CharSet.Auto, SetLastError = true)] static extern int GetWindowTextLength(IntPtr hWnd); static void Main() { IntPtr thisWindow = FindWindow(null, "Окно"); IntPtr Rod = thisWindow; IntPtr otherWindow = FindWindowEx(Rod, IntPtr.Zero, "RICHEDIT50W", null); int len = GetWindowTextLength(otherWindow); StringBuilder sb = new StringBuilder(len); len = GetWindowText(otherWindow, sb, len); Console.WriteLine(sb.ToString(0,len)); //выводит пустую строку Console.WriteLine(thisWindow); //выводлит дескриптор родителя Console.WriteLine(otherWindow);//выводит дескриптор приемника Console.ReadKey(true); } } } Please help, how to get title "otherWindow"? 
  • len = len * 2; and in GetWindowText instead of len, pass sb.Capacity - an example of c # here - Stack

1 answer 1

According to MSDN :

To get a message in the process of another process, send a message to the WM_GETTEXT message.

If you are trying to get the text of a control in another process (and the name RICHEDIT50W assumes that this is so), you need to send WM_GETTEXT directly.

 [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam); 


 const int WM_GETTEXT = 0xD; StringBuilder buffer = new StringBuilder(65535); SendMessage(hWnd_of_Notepad_Editor, WM_GETTEXT, buffer.Length, buffer);