End Google Ads 201810 - BS.net 01 --> I want to get the caret position with my C# application.but in some cases ,GetGuiThreadInfo does not work creectly.

it works well on

1. notepad
2. ie
3. explorer
4. word

and works bad on (hwndCaret will be 0)

1. Visual Studio
2. Firefox
3. Sublime Text 2

Anyone who can helps me?

public partial class Form1 : Form { [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); [DllImport("user32.dll")] static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui); [StructLayout(LayoutKind.Sequential)] public struct GUITHREADINFO { public int cbSize; public int flags; public IntPtr hwndActive; public IntPtr hwndFocus; public IntPtr hwndCapture; public IntPtr hwndMenuOwner; public IntPtr hwndMoveSize; public IntPtr hwndCaret; public Rectangle rectCaret; } public GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd) { if (hwnd != IntPtr.Zero) { uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero); GUITHREADINFO guiThreadInfo = new GUITHREADINFO(); guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo); if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false) return null; return guiThreadInfo; } return null; } public void SendText(string text) { IntPtr hwnd = GetForegroundWindow(); if (String.IsNullOrEmpty(text)) return; GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd); AddMessage("go"); if (guiInfo != null) { IntPtr ptr = (IntPtr)guiInfo.Value.hwndCaret; AddMessage("hwndCaret = " + ptr.ToInt32()); if (ptr != IntPtr.Zero) { AddMessage("Left = " + guiInfo.Value.rectCaret.Left); for (int i = 0; i < text.Length; i++) { SendMessage(ptr, 0x102, (IntPtr)(int)text[i], IntPtr.Zero); } } } } public Form1() { InitializeComponent(); } private void AddMessage(string message) { var i = listBox1.Items.Add(message); listBox1.SelectedIndex = i; } private void button1_Click(object sender, EventArgs e) { Thread.Sleep(2000); //textBox1.Focus(); SendText("Abcdefg"); AddMessage("------------"); } }