micropathic Posted November 25, 2004 Posted November 25, 2004 Hi, I am looking for a way to grab the ULR from the Address bar in an IE window. Does anyone know of a way I could do this? Thanks for any help! Quote
micropathic Posted November 25, 2004 Author Posted November 25, 2004 (edited) Well, I have been looking into doing this with Win32 API calls. Here is the code I've been trying to make work: 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 FindWindow Lib "user32" Alias "FindWindowA"(ByVal lpClassName As String, ByVal lpWindowName As String) 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 Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sCaption As New VB6.FixedLengthString(256) 'Dim String w/ 256 Spaces In Memory, Is There A .Net Way To Do This? Dim hwnd As Long = FindWindow("IEFrame", vbNullString) 'Finds IE Window By Container Name, "IEFrame" If Not hwnd = 0 Then 'Tests If Window Exists SetForegroundWindow(hwnd) 'Brings Window To Foreground GETWINDOWTEXT(hwnd, sCaption.Value, 256) 'Get The Text From That Window, For Testing Code... Not Functionally Needed MsgBox(sCaption.Value) 'Show The Value Of The Caption, For Testing Code... Not Functionally Needed 'The Following 5 Lines Of Code Are What Seem To Be Causing An Issue. 'I'm Trying To "Drill" Into The IE Window And Search For The URL Address 'Bar Portion Of The Window. This Doesn't Seem To Be Working Correctly. 'What Am I Doing Wrong With These FindWindowEx's? Dim Worker As Long = FindWindowEx(hwnd, 0, "WorkerW", vbNullString) 'Supposed To Find The Component W/ The Caption "WorkerW" Within The IE Window Dim ToolBar As Long = FindWindowEx(Worker, 0, "rebarwindow32", vbNullString) 'Supposed To Find The Component W/ The Caption "rebarwindow32" Within The "WorkerW" Component Dim ComboBoxEx As Long = FindWindowEx(ToolBar, 0, "comboboxex32", vbNullString) 'Supposed To Find The Component W/ The Caption "comboboxex32" Within The "rebarwindow32" Component Dim Combo As Long = FindWindowEx(ComboBoxEx, 0, "combobox", vbNullString) 'Supposed To Find The Component W/ The Caption "combobox" Within The "comboboxex32" Component Dim Edit As Long = FindWindowEx(Combo, 0, "Edit", vbNullString) 'Supposed To Find The Component W/ The Caption "Edit" (This Is Actually The URL Address Bar From Which I'm Trying To 'Pull The Text From) Within The "combobox" Component 'Here Is Where I Would Like To Pull The Text From The "Edit" 'Component (URL Address Bar Text) And Then Show The Value In 'A Message Box End If End Sub At this point I am a little lost, so any help would be super valuable to me! Edited November 26, 2004 by micropathic Quote
micropathic Posted November 26, 2004 Author Posted November 26, 2004 Got this to work with this code: 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 Quote
Recommended Posts