开发者

Determine IE window current URL programmatically without BHO

开发者 https://www.devze.com 2023-02-16 11:16 出处:网络
Could someone guide me in the direction of how I would determine IE\'s current URL programmatically without a BHO?

Could someone guide me in the direction of how I would determine IE's current URL programmatically without a BHO?

The only way I have been able to think of accomplishing this sort of functionality is by looking at the title for the window rather then the URL but this seems hacky.

On the other hand I assume that the textbox that stores the URL would have a handle which I attach to and read the text. Am I correct in this assumption?

Any guidance would be appreciated.

PS: Bonus points if you can provide an example/guidance that will work with Firefox also.

EDIT: OK so further research is starting to unc开发者_开发技巧over that what I need to look at is GetWindowText and the message WM_GETTEXT. If I get this all worked out will post answer.


So this is a REALLLLLY old post, but I stumbled upon it so figured I would attempt to answer it since I just learned this :)

You can do it using the Windows Shell. You can iterate through the open windows and look for any "HTTPDocumentClass" object (these are Internet Explorer windows) and then you can access the .LocationUrl member to find out the URL.

I don't know how to write an example in C#, but here's how you do it in VB.
NOTE: You need to add references to Microsoft Shell Controls and Automation and Microsoft Internet Controls.

Imports Shell32
Imports SHDocVw

Public Function GetIExplorerURL() As String()
    Dim exShell As New Shell32.Shell
    Dim URLs As New List(Of String)
    For Each window As SHDocVw.ShellBrowserWindow In DirectCast(exShell.Windows, SHDocVw.IShellWindows)
        If TypeName(window.Document) = "HTMLDocumentClass" Then
            URLs.Add(window.LocationURL)
        End If
    Next

    Return URLs.ToArray
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消