3

Does anybody know how I can get the URL of any open IE processes on a computer? I don't need to manipulate the IE instance at all -- just get information about the page that's currently loaded.

Thanks!

carlbenson
  • 3,177
  • 5
  • 35
  • 54
  • 1
    Keep in mind an IE process might have several tabs as well.. I think this information is exposed through COM automation but I have no idea where you'd even start. – Mike Christensen Oct 11 '11 at 21:28
  • @Mike That's a great point, but it's actually not relevant for my task, so there's no need to worry about it...... yet. – carlbenson Oct 11 '11 at 21:29
  • Maybe, it would be easier to trace the urls using a proxy. http://stackoverflow.com/questions/7671552/how-can-i-monitor-http-traffic-and-get-a-list-of-urls-in-c/7671959#comment9344319_7671959 – L.B Oct 11 '11 at 21:39

3 Answers3

1

This appears to be one way of doing it (code is Visual Basic, sorry, but it shows the principle):

Private Declare Function GETWINDOWTEXT Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
Private Const WM_GETTEXT As Short = &HDS
Private Const WM_GETTEXTLENGTH As Short = &HES

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim hwnd As Integer = FindWindowEx(0, 0, "IEFrame", vbNullString)

        If Not hwnd = 0 Then
            SetForegroundWindow(hwnd)

            Dim Worker As Integer = FindWindowEx(hwnd, 0, "WorkerW", vbNullString)
            Dim ToolBar As Integer = FindWindowEx(Worker, 0, "ReBarWindow32", vbNullString)
            Dim ComboBoxEx As Integer = FindWindowEx(ToolBar, 0, "ComboBoxEx32", vbNullString)

            Dim txtLength As Long = SendMessage(ComboBoxEx, WM_GETTEXTLENGTH, CInt(0), CInt(0)) + 1  ' Get Length Of Text
            Dim txtBuff As String = Space(txtLength)
            Dim URL As Long = URL = SendMessage(ComboBoxEx, WM_GETTEXT, txtLength, txtBuff) 'Get URL From ComboBoxEx

            MsgBox(txtBuff)
        End If
End Sub

Basically, you're finding the IE window, then drilling down to find the combobox in which the URL is typed, and then getting whatever string is typed into it. Obviously this isn't a perfect approach (if somebody overwrites the URL but doesn't hit Enter, you wouldn't know it).

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • C# doesn't understand the &HDS in public static extern short WM_GETTEXT = &HDS. What is that? How do I stop the error "The name HDS does not exist in the current context"? Thanks! – carlbenson Oct 12 '11 at 21:12
  • @Carl: `MusiGenesis` also doesn't understand what `&HDS` is supposed to be. Sorry, it was found code - I've never run it myself. – MusiGenesis Oct 12 '11 at 23:44
  • 1
    @carlbenson in Basic &H means hex value. So it is just hex D and hex E (and the S stands for "short"). – Anixx Nov 26 '14 at 01:46
1

A simple solution, and it works: http://omegacoder.com/?p=63

carlbenson
  • 3,177
  • 5
  • 35
  • 54
0
  • Start IE yourself through automation (i.e var oIE = WScript.CreateObject("InternetExplorer.Application", "IE_");) and listen to NavigateComplete2.

  • Peek inot ROT (running objects table) - I think IE documents should show up there - Win32/COM - http://msdn.microsoft.com/en-us/library/ms684004(VS.85).aspx

  • Just find all IE windows and take text from address well (see MusiGenesis answer for that).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179