473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Fetching the url of an external Internet Explorer

258 100+
I'v been looking around to find a way to fetch the current URL in an external IE.

I finaly found a way to do it but for some reason it doesn't work. It might be because of my IE version. I tested the program on IE6 and IE7 but none of them work

The program uses :
findWindow
getClassName
sendMessage
May 30 '10 #1
10 2219
QVeen72
1,445 Expert 1GB
Hi,

Can you post the code..?
May 31 '10 #2
vb5prgrmr
305 Expert 100+
After FindWindow to find the parent window (i.e. IE), you need to use EnumChildWindows to look for the combo box. Once you have the handle to the combo box, you can use the SendMessage API with the WM_GETTEXT const...

Also: Spy++ would be of great help here...



Good Luck
May 31 '10 #3
bnashenas1984
258 100+
Hi and thanks for replying my post.
Actually I solved the issue by checking the title of the window every 10 ms.
IE changes it's title to the url it's opening before loading the page.

But anyways finding a logical way to solve this problem would be great.

Heres my code

Module
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Type ProcData
  4.     AppHwnd As Long
  5.     title As String
  6.     Placement As String
  7.     Left As Long
  8.     Top As Long
  9.     Right As Long
  10.     Bottom As Long
  11. End Type
  12.  
  13. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  14.  
  15. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  16. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  17. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  18. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  19.  
  20. Private Const WM_GETTEXT = &HD
  21. Private Const WM_GETTEXTLENGTH = &HE
  22.  
  23. Private Const GW_CHILD = 5
  24. Private Const GW_HWNDNEXT = 2
  25. Private Const GW_HWNDFIRST = 0
  26.  
  27. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  28.  
  29. Public g_Contains As String
  30.  
  31.  
  32. ' If this window is of the Edit class, return
  33. ' its contents. Otherwise search its children
  34. ' for an Edit object.
  35. Public Function EditInfo(window_hwnd As Long) As String
  36. Dim txt As String
  37. Dim buf As String
  38. Dim buflen As Long
  39. Dim child_hwnd As Long
  40. Dim children() As Long
  41. Dim num_children As Integer
  42. Dim i As Integer
  43.  
  44.     ' Get the class name.
  45.     buflen = 256
  46.     buf = Space$(buflen - 1)
  47.     buflen = GetClassName(window_hwnd, buf, buflen)
  48.     buf = Left$(buf, buflen)
  49.  
  50.     ' See if we found an Edit object.
  51.     If buf = "Edit" Then
  52.         EditInfo = WindowText(window_hwnd)
  53.         Exit Function
  54.     End If
  55.  
  56.     ' It's not an Edit object. Search the children.
  57.     ' Make a list of the child windows.
  58.     num_children = 0
  59.     child_hwnd = GetWindow(window_hwnd, GW_CHILD)
  60.     Do While child_hwnd <> 0
  61.         num_children = num_children + 1
  62.         ReDim Preserve children(1 To num_children)
  63.         children(num_children) = child_hwnd
  64.  
  65.         child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
  66.     Loop
  67.  
  68.     ' Get information on the child windows.
  69.     For i = 1 To num_children
  70.         txt = EditInfo(children(i))
  71.         If txt <> "" Then Exit For
  72.     Next i
  73.  
  74.     EditInfo = txt
  75. End Function
  76. ' ************************************************
  77. ' Return the text associated with the window.
  78. ' ************************************************
  79. Public Function WindowText(window_hwnd As Long) As String
  80. Dim txtlen As Long
  81. Dim txt As String
  82.  
  83.     WindowText = ""
  84.     If window_hwnd = 0 Then Exit Function
  85.  
  86.     txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, 0)
  87.     If txtlen = 0 Then Exit Function
  88.  
  89.     txtlen = txtlen + 1
  90.     txt = Space$(txtlen)
  91.     txtlen = SendMessage(window_hwnd, WM_GETTEXT, txtlen, ByVal txt)
  92.     WindowText = Left$(txt, txtlen)
  93. End Function
  94.  
  95.  
  96. Public Function EnumProc(ByVal app_hwnd As Long, ByVal lParam As Long) As Boolean
  97. Dim buf As String * 1024
  98. Dim title As String
  99. Dim length As Long
  100. Dim txt As String
  101.  
  102.     ' Get the window's title.
  103.     length = GetWindowText(app_hwnd, buf, Len(buf))
  104.     title = Left$(buf, length)
  105.  
  106.     ' See if the title contains the target text.
  107.     If InStr(title, g_Contains) > 0 Then
  108.         ' This is the target. Search for the desired path.
  109.         If FollowChildPath(txt, app_hwnd, LCase$("IEFrame/WorkerA/ReBarWindow32/ComboBoxEx32/ComboBox/Edit/")) Then
  110.             frmWindowList.txtAddress.Text = txt
  111.         Else
  112.             frmWindowList.txtAddress.Text = "<not found>"
  113.         End If
  114.  
  115.         ' Stop searching.
  116.         EnumProc = 0
  117.     Else
  118.         ' Continue searching until we find it.
  119.         EnumProc = 1
  120.     End If
  121. End Function
  122.  
  123. ' Search this window's children for the specified path.
  124. ' If we find the path, return the final window's text
  125. ' through parameter window_text and return True.
  126. Public Function FollowChildPath(ByRef window_text As String, ByVal window_hwnd As Long, ByVal target_path As String, Optional ByVal current_path As String = "") As Boolean
  127. Dim txt As String
  128. Dim buf As String
  129. Dim buflen As Long
  130. Dim child_hwnd As Long
  131. Dim children() As Long
  132. Dim num_children As Integer
  133. Dim i As Integer
  134.  
  135.     ' Assume we will not find the target path.
  136.     FollowChildPath = False
  137.  
  138.     ' Get the class name.
  139.     buflen = 256
  140.     buf = Space$(buflen - 1)
  141.     buflen = GetClassName(window_hwnd, buf, buflen)
  142.     buf = Left$(buf, buflen)
  143.  
  144.     ' Add the class name to the current path.
  145.     current_path = current_path & LCase$(buf) & "/"
  146.  
  147.     ' See if we're on the right track.
  148.     If Left$(target_path, Len(current_path)) <> current_path Then
  149.         ' We are off the path. Don't search it.
  150.     ElseIf target_path = current_path Then
  151.         ' We are on the path and have reached the end.
  152.         ' Set the window's text and stop searching.
  153.         If buf = "Edit" Then
  154.             window_text = WindowText(window_hwnd)
  155.         Else
  156.             window_text = "<not an Edit window>"
  157.         End If
  158.  
  159.         FollowChildPath = True
  160.     Else
  161.         ' We are on the path but have not reached the end.
  162.         ' Make a list of the child windows.
  163.         num_children = 0
  164.         child_hwnd = GetWindow(window_hwnd, GW_CHILD)
  165.         Do While child_hwnd <> 0
  166.             num_children = num_children + 1
  167.             ReDim Preserve children(1 To num_children)
  168.             children(num_children) = child_hwnd
  169.  
  170.             child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
  171.         Loop
  172.  
  173.         ' Search the child windows.
  174.         For i = 1 To num_children
  175.             If FollowChildPath(window_text, children(i), target_path, current_path) Then
  176.                 ' We found the result. Stop searching.
  177.                 FollowChildPath = True
  178.                 Exit For
  179.             End If
  180.         Next i
  181.     End If
  182. End Function
  183.  
  184.  

Form load :

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam As Long) As Long
  4. ' Start the enumeration.
  5. Private Sub cmdFindAddress_Click()
  6.     g_Contains = " - Microsoft Internet Explorer"
  7.     EnumWindows AddressOf EnumProc, 0
  8. End Sub
  9.  
  10. Private Sub Form_Resize()
  11.     txtAddress.Width = ScaleWidth
  12. End Sub
  13.  
May 31 '10 #4
vb5prgrmr
305 Expert 100+
>>After FindWindow to find the parent window (i.e. IE), you need to use EnumChildWindows to look for the combo box...
May 31 '10 #5
bnashenas1984
258 100+
Hi again
Thank you vb5prgrmr for the reply.
I found another program which grabs all the child windows of selected window (by hWnd)

BUT the problem is that it doesn't contain the url of IE.

Here is my code :


Module :
Expand|Select|Wrap|Line Numbers
  1. Private Declare Function EnumChildWindows Lib "user32" ( _
  2.    ByVal hWndParent As Long, ByVal lpEnumFunc As Long, _
  3.    ByVal lParam As Long) As Long
  4.  
  5. Private WndList() As Long
  6. Private NumWnd As Long
  7.  
  8. ' Get a list of windows within a specific window
  9. Public Function ListWindows( _
  10.    ByVal inWnd As Long) As Long() 'HWND[]
  11.    NumWnd = 0
  12.    Erase WndList
  13.    Call EnumChildWindows(inWnd, AddressOf EnumChildProc, 0&)
  14.  
  15.    ListWindows = WndList
  16. End Function
  17.  
  18. ' Mange the call-back's from the API
  19. Private Function EnumChildProc( _
  20.    ByVal hWnd As Long, ByVal lParam As Long) As Long
  21.    ReDim Preserve WndList(NumWnd) As Long
  22.    WndList(NumWnd) = hWnd
  23.    NumWnd = NumWnd + 1
  24.    EnumChildProc = 1
  25. End Function
  26.  
  27.  

Form_load :

Expand|Select|Wrap|Line Numbers
  1. Private Declare Function GetForegroundWindow Lib "user32" () As Long
  2. Private Declare Function GetWindowTextLength Lib "user32" Alias _
  3.    "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
  4. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  5.  
  6. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
  7.    (ByVal hWnd As Long, ByVal lpClassName As String, _
  8.    ByVal nMaxCount As Long) As Long
  9.  
  10. Private Sub Form_Load()
  11.    Dim Windows() As Long
  12.    Dim NumWnds As Long, LoopWnds As Long
  13.    Dim WndName As String, ClassName As String
  14.  
  15.    Const ClassLength As Long = 1024
  16.  
  17.    ' Get a list of windows within the current window
  18.    Windows() = ListWindows(GetForegroundWindow)
  19.  
  20.    On Error Resume Next ' How many were returned?
  21.    NumWnds = UBound(Windows()) + 1
  22.    On Error GoTo 0
  23.  
  24.    If (NumWnds > 0) Then ' Loop through each
  25.        For LoopWnds = 0 To NumWnds - 1
  26.            ' Get window name and class strings
  27.            ClassName = Space$(ClassLength)
  28.            WndName = Space$(GetWindowTextLength( _
  29.                Windows(LoopWnds)) + 1)
  30.            Call GetWindowText(Windows(LoopWnds), _
  31.                WndName, Len(WndName))
  32.            Call GetClassName(Windows(LoopWnds), _
  33.                ClassName, ClassLength)
  34.  
  35.            ' Display returned results
  36.            Debug.Print "0x" & _
  37.                Hex(Windows(LoopWnds)) & " - """ & _
  38.                TrimNull(WndName) & """ (" & _
  39.                TrimNull(ClassName) & ")"
  40.        Next LoopWnds
  41.    End If
  42. End Sub
  43.  
  44. Private Function TrimNull(ByVal inString As String) As String
  45.    Dim NullPos As Long
  46.  
  47.    ' Trim a null-terminated string
  48.    NullPos = InStr(1, inString, vbNullChar)
  49.    If (NullPos) Then _
  50.        TrimNull = Left$(inString, NullPos - 1) Else _
  51.        TrimNull = inString
  52. End Function
  53.  
  54.  

Please help
I'v been searching whole day

Thanks
Jun 3 '10 #6
QVeen72
1,445 Expert 1GB
Hi,

How about this :

Add a Command Button and a ListBox Control on the form, and write this in Command_Click event:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     Dim oWin As Object, oShApp As Object
  3.     List1.Clear
  4.     Set oShApp = CreateObject("Shell.Application")
  5.     For Each oWin In oShApp.Windows
  6.         If TypeName(oWin.document) = "HTMLDocument" Then
  7.            List1.AddItem oWin.document.URL
  8.         End If
  9.     Next
  10.     Set oShApp = Nothing
  11. End Sub
  12.  
Regards
Veena
Jun 4 '10 #7
bnashenas1984
258 100+
@QVeen72
Oh my god
You are a life saver.

How can I thank you?
I was going to hang my self when I tested this script

Thanks again :)
Jun 4 '10 #8
QVeen72
1,445 Expert 1GB
Hi,

You are Welcome :)

Regards
Veena
Jun 4 '10 #9
bnashenas1984
258 100+
@QVeen72
Hi again
I'm using your script in a timer (interval=10) to grab and list a history of all websites the user has visited.

It's working fine now but there is one problem.

Sometimes when I open and close a browser I get this error :

Runtime error '-2147467259 (80004005)':
Automation error
Unspecified error

Do you know why this happens?
Jun 4 '10 #10
QVeen72
1,445 Expert 1GB
Hi,

Not sure.. may be, when the VB Script is running, the user is closing the window or something...

Just Write "On Error Resume Next" in the Vb code..

Regards
Veena
Jun 4 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: mic | last post by:
I'm currently trying to write an MSIE automation software and have run into such problem - have no idea how to take control of external popup windows - I use WebBrowser COM object and am able to...
12
by: SunshineGirl | last post by:
I'm trying to receive events from a running instance of Internet Explorer. So far I've only been able to receive events from an instance I launch from code, but I need to receive events from all...
0
by: Nick | last post by:
We develop and host a webbased accounting application which is fully xml/xsd and webservice oriented. We have recently noticed that xml schemas (xsd or xdr) are not being cached by Internet...
3
by: VK | last post by:
Internet Explorer 7 beta 2 preview CNET Editor review: <http://reviews.cnet.com/Internet_Explorer_7_for_XP_SP2_Beta_2/4505-3514_7-31454661-2.html?tag=nl.e415> Summary (my personal review...
2
by: Jim S | last post by:
Is there a way in .Net or via interop (com etc.) for an external application (a .net winform for example) to read the html contents (or the DOM) of a running instance or internet explorer? I...
1
by: CWPinpoint | last post by:
As members of this group may include users or associates of 3B2 (APP), we thought you may be interested to see our December newsletter which includes a review of the recent PTC World user meeting...
9
by: Etayki | last post by:
Hi! I am new to VB.net and I am using the Visual Basic 2005 Express Edition I have two questions: 1. I am trying to write an application that will automate Internet Explorer and store data...
1
by: =?Utf-8?B?Tm9ybQ==?= | last post by:
I have an apppliation running on Windows Server 2003. It uses a user control dll. When the application is run from the server with localhost everything runs fine. If the application is run from...
1
by: avpkills2002 | last post by:
I seem to be getting this weird problem in Internet explorer. I have written a code for parsing a XML file and displaying the output. The code works perfectly fine with ffx(Firefox).However is not...
2
by: Federico Cozzi | last post by:
Hello, in my VB.net I would like to 1) start a new Internet Explorer process (not a new window of the running process) 2) and connecting to its WebBrowser control, or InternetExplorerClass,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.