6

How can I get the url from a running instance of firefox using .NET 2.0 windows/console app? C# or VB codes will do.

Thanks!

Leon Tayson
  • 4,741
  • 7
  • 37
  • 36
  • You need to be a bit clearer. Do you mean how to obtain the url from a running instance of firefox in a windows/console app? – Kev Jan 10 '09 at 06:18
  • If this can be done, you realise there could be multiple instances each with multiple tabs. What are you trying to achieve? – Kev Jan 10 '09 at 06:26
  • yes, i do realize that. i have this working with IE, in fact, i have this working with firefox but it's using a .NET 3.5 class but the client wants to have it done in .NET 2.0 – Leon Tayson Jan 10 '09 at 09:00

8 Answers8

6

Building on Rob Kennedy's answer and using NDde

using NDde.Client;

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

NB: This is very slow. It takes a few seconds on my computer. The result will look something like this :

"http://stackoverflow.com/questions/430614/get-firefox-url","Get Firefox URL? - Stack Overflow",""

More info on browser DDE here.

Foole
  • 4,754
  • 1
  • 26
  • 23
  • I was using this piece of code since a couple of year ago. However with the release of a new version of FF it is not working anymore. Do you know if there any other way to achieve the same result? – Ayorus Jan 15 '18 at 23:11
4

For most browsers, including Internet Explorer, Navigator, Firefox, and Opera, the supported and sanctioned way of doing this is to use DDE. The topic name in all of them is WWW_GetWindowInfo; only the name of the target window varies. That technique will be difficult for you, though, because .Net doesn't support DDE. If you can find a way to get around that limitation, you'll be all set.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
2

it seems that this might be difficult, here's some discussion on it: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/c60b1699-9fd7-408d-a395-110c1cd4f297/

John Boker
  • 82,559
  • 17
  • 97
  • 130
1
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr parentHandle,
    IntPtr childAfter, string className, IntPtr windowTitle);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SendMessage(IntPtr hWnd,
        int msg, int wParam, StringBuilder ClassName);

    private static string GetURL(IntPtr intPtr, string programName, out string url)
    {
        string temp=null;
        if (programName.Equals("chrome"))
        {
            var hAddressBox = FindWindowEx(intPtr, IntPtr.Zero, "Chrome_OmniboxView", IntPtr.Zero);
            var sb = new StringBuilder(256);
            SendMessage(hAddressBox, 0x000D, (IntPtr)256, sb);
            temp = sb.ToString();
        } 
        if (programName.Equals("iexplore"))
        {
            foreach (InternetExplorer ie in new ShellWindows())
            {
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(ie.FullName);
                if (fileNameWithoutExtension != null)
                {
                    var filename = fileNameWithoutExtension.ToLower();
                    if (filename.Equals("iexplore"))
                    {
                        temp+=ie.LocationURL + " ";
                    }
                }
            }
        }
        if (programName.Equals("firefox"))
       {
            DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
            dde.Connect();
            string url1 = dde.Request("URL", int.MaxValue);
            dde.Disconnect();
            temp = url1.Replace("\"","").Replace("\0","");
        }
        url = temp;
        return temp;
    }

Please do following to run this code Add Reference > Com > Microsoft.Internet.Controls from VS.NET in your project

Download the bin from http://ndde.codeplex.com/ for DdeClient class and add it to your project

Please Let me know if any issue

Vijay Parmar
  • 795
  • 4
  • 13
1

You may want to check into the source code of WatiN. Their next version is open source and supports firefox, so I would imagine the functionality for doing this is in it.

Jeff Martin
  • 10,812
  • 7
  • 48
  • 74
  • I've edited the question to add more details. I'm doing a winforms application and i need to get the browser URL. I already have the codes for the IE Url. Thanks! – Leon Tayson Jan 10 '09 at 06:17
1

Use MozRepl: https://github.com/bard/mozrepl/wiki/ + mozRepl .NET Connector: http://mozreplconnector.codeplex.com/releases/view/17398

  var connect = new MozReplConnectDotNet.MozReplConnect(4242);
  connect.Connect();
  Console.WriteLine(connect.SendRecieve("gBrowser.currentURI.spec"));
Serj-Tm
  • 16,581
  • 4
  • 54
  • 61
0

Poor man's solution, if anything else fails: activate the Firefox window, send Ctrl+L (activates address bar), send Ctrl+C (copy selection, ie. URL, to clipboard) and read the clipboard.

Lot of issues with this method (among them it does strange stuff for the user if they are in front of the computer) so it is only a backup solution...

PhiLho
  • 40,535
  • 6
  • 96
  • 134
0

try this one:

        //get all running process of firefox
        Process[] procsfirefox = Process.GetProcessesByName("firefox");
        foreach (Process firefox in procsfirefox)
        {
            //the firefox process must have a window
            if (firefox.MainWindowHandle == IntPtr.Zero)
            {
                continue;
            }
            AutomationElement sourceElement = AutomationElement.FromHandle(firefox.MainWindowHandle);
                          
            AutomationElement editBox = sourceElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Search with Google or enter address"));             
            // if it can be found, get the value from the editbox
            if (editBox != null)
            {
                ValuePattern val = ((ValuePattern)editBox.GetCurrentPattern(ValuePattern.Pattern));

                Console.WriteLine("\n Firefox URL found: " + val.Current.Value);
            }

            //-----------------------------find titlebar element for site title---------------------------------// 
            
            AutomationElement elmTitleBar = sourceElement.FindFirst(TreeScope.Descendants,
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar));
            if (elmTitleBar != null)
            
                Console.WriteLine("\n Firefox TitleBar found: " + elmTitleBar.Current.Name);
            }

full source code:https://github.com/Moeedahmad899/GetFirefoxURL