使用SHDocVw挂钩IE选项卡

时间:2015-10-10 15:42:25

标签: c# internet-explorer

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

到目前为止,我已经能够挂钩IE的新实例(当用户打开IE的新窗口时)。但是如果用户在窗口中打开一个标签,我就无法挂钩该标签。

Range("A:E")

1 个答案:

答案 0 :(得分:0)

经过大量的反复试验,我想出了一个解决方案。虽然我不确定它有多有效。但它的确有效。发布以防其他人需要类似的东西。

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; }
}