Get URL from open browser

kutiJack used Ask the Experts™
I want to use clipboard utility, that stores pieces of text copied from internet and it would be nice to add to every piece of text the URL of the page, from which the text was copied. I already have code for monitoring clipboard changes and for processing the clipboard content, but how can I get the current URL from browser?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Consultant - Deloitte

#region WIN32 API
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder sb);
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string _ClassName, string _WindowName);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr _Parent, int _ChildAfter, string _ClassName, string _WindowName);
        public const int WM_GETTEXTLENGTH = 0x000E;
        public const int WM_GETTEXT = 0x000D; 
IntPtr worker = FindWindowEx("pass Window Handle here", 0, "WorkerW", null);
                    IntPtr toolbar = FindWindowEx(worker, 0, "rebarwindow32", null);
                    IntPtr comboboxex = FindWindowEx(toolbar, 0, "comboboxex32", null);
                    IntPtr combo = FindWindowEx(comboboxex, 0, "ComboBox", null);
                    IntPtr edit = FindWindowEx(combo, 0, "Edit", null);
                    StringBuilder sb = new StringBuilder(length + 1);
                    SendMessage(edit, WM_GETTEXT, length + 1, sb);
                    if (sb.ToString().ToLower().IndexOf("") != -1)
                        if (sb.ToString().IndexOf("?") != -1)
                            string url = sb.ToString();

Open in new window


Thank you, after changing some letters to capitals, I got URL from IE. Because I learned, that at the time it is impossible to get URL from Firefox using C#, I consider your solution complete.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial