用SHDocVw挂钩IE选项卡

本文关键字:选项 IE 挂钩 SHDocVw | 更新日期: 2023-09-27 18:17:00

我正在尝试跟踪IE访问的网站。我正在使用SHDocVw.dll。

到目前为止,我已经能够挂钩IE的新实例(当用户打开IE的新窗口时)。但是如果用户在窗口中打开一个选项卡,我就不能钩住那个选项卡。

private static SHDocVw.InternetExplorer browser;
private static SHDocVw.ShellWindows shellWindows;
private static ArrayList Current_IE_Handles;
static void Main(string[] args)
{
    Current_IE_Handles=new ArrayList();
    shellWindows = new SHDocVw.ShellWindowsClass();
    shellWindows.WindowRegistered+=new SHDocVw.DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);
    shellWindows.WindowRevoked+=new SHDocVw.DShellWindowsEvents_WindowRevokedEventHandler(shellWindows_WindowRevoked);
}
// THIS EVENT IS FIRED WHEN THE BROWSER IS JUST ABOUT THE NAVIGATE TO A WEBSITE
private static void browser_BeforeNavigate2(object a,ref object b,ref object c,ref object d,ref object e,ref object f,ref bool g)
{
    try
    {
        bool adbar = (bool)a.GetType().InvokeMember("AddressBar", System.Reflection.BindingFlags.GetProperty, null, a, null);
        if (adbar)
            Console.WriteLine(b);
    }
    catch (Exception)
    {
    }
}
// THIS EVENT IS FIRED WHEN A BROWSER IS CLOSED
private static void shellWindows_WindowRevoked(int z)
{
    string filnam;
    ArrayList Closed_IE=new ArrayList();
    foreach (SHDocVw.InternetExplorer ie in shellWindows)
    {
        filnam = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
        if (filnam.Equals("iexplore"))
            Closed_IE.Add(ie.HWND);
    }
    foreach (int hwnd in Current_IE_Handles)
    {
        if (!Closed_IE.Contains(hwnd))
        {
            Console.WriteLine("Browser Closed : Handle( " + hwnd.ToString("X4") + " )");
            Current_IE_Handles.Remove(hwnd);
            break;
        }
    }
}
// THIS EVENT IS FIRED WHEN A NEW BROWSER IS OPENED
private static void shellWindows_WindowRegistered(int z)
{
    string filnam;
    foreach (SHDocVw.InternetExplorer ie in shellWindows)
    {
        filnam = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
        if (filnam.Equals("iexplore"))
        {
            if (!Current_IE_Handles.Contains(ie.HWND))
            {
                Current_IE_Handles.Add(ie.HWND);
                Console.WriteLine("Browser Open : Handle( " + ie.HWND.ToString("X4") + " )");
                //ie.BeforeNavigate2 += new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(browser_BeforeNavigate2);
                ie.NavigateComplete2 += browser_NavigateComplete2;
            }
        }
    }
}
static void browser_NavigateComplete2(object pDisp, ref object URL)
{
    try
    {
        bool adbar = (bool)pDisp.GetType().InvokeMember("AddressBar", System.Reflection.BindingFlags.GetProperty, null, pDisp, null);
        if (adbar)
            Console.WriteLine(URL);
    }
    catch (Exception)
    {
    }
}

用SHDocVw挂钩IE选项卡

经过反复试验,我想出了一个解决方案。虽然我不确定它有多有效。但它是有效的。把它贴出来,以防别人需要类似的东西。

private static List<IEContainer> Current_IEs;
private static SHDocVw.ShellWindows shellWindows;
static void Main(string[] args)
{
    Current_IEs = new List<IEContainer>();
    shellWindows = new SHDocVw.ShellWindowsClass();
    shellWindows.WindowRegistered+=new SHDocVw.DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);
    shellWindows.WindowRevoked+=new SHDocVw.DShellWindowsEvents_WindowRevokedEventHandler(shellWindows_WindowRevoked);
    while (true)
        Console.ReadLine();
}
// THIS EVENT IS FIRED WHEN THE A NEW BROWSER IS CLOSED
private static void shellWindows_WindowRevoked(int z)
{
    if (Current_IEs.Exists(x => x.Cookie == z))
    {
        Console.WriteLine("Browser close: " + z);
        Current_IEs.Remove(Current_IEs.Find(x => x.Cookie == z));
    }
}
// THIS EVENT IS FIRED WHEN THE A NEW BROWSER IS OPEN
private static void shellWindows_WindowRegistered(int z)
{
    string filnam;
    foreach (SHDocVw.InternetExplorer ie in shellWindows)
    {
        filnam = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
        if (filnam.Equals("iexplore"))
        {
            if (!Current_IEs.Exists(x => x.IE == ie))
            {
                try
                {
                    bool adbar = (bool)ie.GetType().InvokeMember("AddressBar", System.Reflection.BindingFlags.GetProperty, null, ie, null);
                    if (adbar)
                    {
                        Current_IEs.Add(new IEContainer { Cookie = z, IE = ie });
                        Console.WriteLine("Browser open: " + z);
                        ie.NavigateComplete2 += browser_NavigateComplete2;
                    }
                }
                catch (Exception)
                {
                }
            }
        }
    }
}
static void browser_NavigateComplete2(object pDisp, ref object URL)
{
    try
    {
        bool adbar = (bool)pDisp.GetType().InvokeMember("AddressBar", System.Reflection.BindingFlags.GetProperty, null, pDisp, null);
        if (adbar)
            Console.WriteLine(URL);
    }
    catch (Exception)
    {
    }
}
class IEContainer
{
    public int Cookie { get; set; }
    public SHDocVw.InternetExplorer IE { get; set; }
}