vb.net 获取浏览器中打开的标签页的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13307946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the URLs of opened tabs in browser
提问by Mohammad Fatafta
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, ...)
我正在做一个项目,我需要获取浏览器(例如 Google Chrome、IE、Firefox 等)中所有打开的标签的 URL
Is there any way to do that using c# or vb.net?
有没有办法使用 c# 或 vb.net 做到这一点?
p.s. it is a windows form application
ps它是一个windows窗体应用程序
回答by famf
ie:
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#:
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#:
火狐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)
下载 NDde.2.01.0563 (NDde.dll)
I've also made to Chrome:
我也做了 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)

