Hack Forums
[C#] Get Default Browser + URL - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Get Default Browser + URL (/showthread.php?tid=4344176)



Get Default Browser + URL - frapppp - Jul 17, 2014

I posted something somewhere on here a while ago and a few people contacted me asking me for help dealing with browsers. I forgot I had this and found it earlier, so figured I could post it up here for anyone that'd find it useful.

Code
class browserHandler
    {
        genericHandler gHandler = new genericHandler();

        public string getBrowser()
        {
            const string userChoice = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice";
            string progId;
            
            using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(userChoice))
            {
                if (userChoiceKey == null)
                {
                    return "unsupported";
                }
                object progIdValue = userChoiceKey.GetValue("Progid");
                if (progIdValue == null)
                {
                    return "unsupported";
                }
                progId = progIdValue.ToString();
                switch (progId)
                {
                    case "IE.HTTP":
                        return "iexplore";
                    case "FirefoxURL":
                        return "firefox";
                    case "ChromeHTML":
                        return "chrome";
                    default:
                        return "unsupported";
                }
            }
        }

        public string getFFURL()
        {
            DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
            dde.Connect();
            string url = dde.Request("URL", int.MaxValue);
            dde.Disconnect();
            return url;
        }

        public string getIEURL()
        {
            genericHandler gHandler = new genericHandler();
            string fileName;

            foreach (InternetExplorer ie in new ShellWindows())
            {
                fileName = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
                if ((gHandler.isInteresting(ie.LocationURL.ToString()) && (fileName.Equals("iexplore"))))
                { return ie.LocationURL.ToString(); }
                else { continue; }
            }
            
            return null;
        }

        public string getChromeURL()
        {
            Process[] procsChrome = Process.GetProcessesByName("chrome");
            foreach (Process chrome in procsChrome)
            {
                // the chrome process must have a window
                if (chrome.MainWindowHandle == IntPtr.Zero)
                {
                    continue;
                }

                // find the automation element
                AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
                AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
                new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));

                // if it can be found, get the value from the URL bar
                if (elmUrlBar != null)
                {
                    AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
                    if (patterns.Length > 0)
                    {
                        ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
                        if (!val.Current.Value.Contains("https"))
                        {
                            string link = "http://" + val.Current.Value;
                           // gHandler.logMsg("Found [" + link + "]");
                            return link;
                        }
                        else
                        {
                            gHandler.logMsg("Found [" + val.Current.Value + "]");
                            return val.Current.Value;
                        }
                    }
                    else { return "error"; }                
                }
            }

            return "error";
        }
    }

Ignore my logging stuff. Any questions feel free to post.


* - sh@rp - Jul 18, 2014

partly copied from this link :

http://en.lmgtfy.com/?q=Get+default+browser+C%23

Quote: DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
dde.Connect();


you don't understand the code and you have forgotten to copy the half ---> where is the DdeClient class ???


* - camelCase - Sep 7, 2015

This way works but is horribly efficient. There is no proper way that I am aware of without hooking, which for modern browsers requires an extension to be added into the browser for that program.

My apologies for ing an old thread.