Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Get Default Browser + URL

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.
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 ???
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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by MatriculateMar 5, 2016 07:03 AM 6 235 Mar 5, 2016 07:03 AM
Last Post: Ninja
[.NET] by DarkMethodsAug 17, 2015 03:32 AM 1 240 Aug 17, 2015 03:32 AM
Last Post: Aristocles
[.NET] by ShikatanaiJun 17, 2015 07:21 PM 12 565 Jun 17, 2015 07:21 PM
Last Post: Shikatanai