3

I'm working on a project, and I need to get the URLs of all opened tabs in browsers (such as Google Chrome, IE, Firefox, ...)

Is there any way to do that using c# or vb.net?

p.s. it is a windows form application

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mohammad Fatafta
  • 86
  • 1
  • 1
  • 5
  • This would be extremely difficult to do. It might help if you explained why you want to do this in case there's a better way of doing it. – PhonicUK Nov 09 '12 at 12:20
  • 2
    I wouldn't make my browser share that information, if I created one. Does not seem secure to me... – kratenko Nov 09 '12 at 12:21
  • Its specific to the browser and of dubious reliability; http://stackoverflow.com/questions/7814027/how-can-i-get-urls-of-open-pages-from-chrome-and-firefox note the comments relating to the proxy server approach. – Alex K. Nov 09 '12 at 15:03

1 Answers1

5

ie:

    Dim browser As SHDocVw.InternetExplorer
    Dim myLocalLink As String
    Dim myDoc As mshtml.IHTMLDocument2
    Dim shellWindows As SHDocVw.ShellWindows = New SHDocVw.ShellWindows()
    Dim filename As String

    For Each ie As SHDocVw.InternetExplorer In shellWindows
        filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower()

        If filename = "iexplore" Then
            browser = ie
            myDoc = browser.Document
            myLocalLink = myDoc.url
            MessageBox.Show(myLocalLink)
        End If
    Next

c#:

        SHDocVw.InternetExplorer browser;
        string myLocalLink;
        mshtml.IHTMLDocument2 myDoc;
        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
        string filename;
        foreach (SHDocVw.InternetExplorer ie in shellWindows)
        {
            filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
            if ((filename == "iexplore"))
            {
                browser = ie;
                myDoc = browser.Document;
                myLocalLink = myDoc.url;
                MessageBox.Show(myLocalLink);
            }

you need:

microsoft.mshtml

and

shdocvw.dll

FireFox c#:

using NDde.Client;


        DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
        dde.Connect();
        string url = dde.Request("URL", int.MaxValue);
        dde.Disconnect();
        MessageBox.Show(url);

Download NDde.2.01.0563 (NDde.dll)

I've also made to Chrome:

Vb.net:

Shared Function:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
 ByVal lpClassName As String, _
 ByVal lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                  ByVal childAfter As IntPtr, _
                  ByVal lclassName As String, _
                  ByVal windowTitle As String) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function


    Dim h As IntPtr
    For Each p As Process In Process.GetProcessesByName("chrome")
        h = FindWindow("Chrome_WidgetWin_1", p.MainWindowTitle)
        Exit For
    Next
    Dim urlH As IntPtr
    urlH = FindWindowEx(h, 0, "Chrome_OmniboxView", Nothing)
    Dim urlHH As IntPtr = Marshal.AllocHGlobal(100)
    Dim NumText As Integer = SendMessage(urlH, &HD, 50, urlHH)
    Dim url As String = Marshal.PtrToStringUni(urlHH)
    MsgBox(url)
famf
  • 2,225
  • 19
  • 17