Capturing NewWindow3 event for shdocvw.InternetExplorer

I want to automate Internet Explorer using C #. So I wrote a simple console application that creates a new instance of InternetExplorer and then logs on to some events.

The following events are working properly: OnQuit, BeforeNavigate2andNewWindow2

But NewWindow3no. Everything compiles and the program can be launched, but after an exception is called when you open the link in another window:Eine Ausnahme (erste Chance) des Typs "System.ArgumentException" ist in mscorlib.dll aufgetreten.

So what am I doing wrong with this event? I used the parameters specified inDWebBrowserEvents2_NewWindow3EventHandler

edit: It seems this is a bug in the library, is there any way to create my own EventHandler Method / Callback method? I did some research and found this page: How to add an event listener using addEventListener MSHTML in IE9? Where someone creates a COM class with a callback method.

=> How can I extend the InternetExplorer class so that I can access NewWindow3EventHanlder?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SHDocVw;
using MSHTML;
using Microsoft.VisualBasic;

namespace BrowserControl
{
    class Program
    {
        private ManualResetEvent closed;
        private InternetExplorer ie;

        public Program()
        {
            closed = new ManualResetEvent(false);
        }

        private void setupIE(InternetExplorer ie = null)
        {
            if (ie == null)
            {
                this.ie = ie = new InternetExplorer();
                Console.WriteLine(Information.TypeName(ie)+" "+Information.TypeName(ie.Application));
            }

            ie.NewWindow3 += new DWebBrowserEvents2_NewWindow3EventHandler(NewWindow3);
            ie.NewWindow2 += new DWebBrowserEvents2_NewWindow2EventHandler(NewWindow2);
            ie.BeforeNavigate2 += BeforeNavigate2;
            ie.OnQuit += OnQuit;

            ie.Visible = true;
        }

        public void NewWindow2(ref object ppDisp, ref bool Cancel)
        {
            Console.WriteLine("new window 2");
        }
        public void NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
        {
            Console.WriteLine("new window 3");
        }
        public void BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
        {
            IWebBrowser2 cie = (IWebBrowser2)pDisp;
            Console.WriteLine(cie.LocationURL + " navigates to " + URL + " target=" + TargetFrameName + " ...");
        }
        public void OnQuit()
        {
            Console.WriteLine("quit");
            closed.Set();
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            Console.WriteLine("Starting Browser ...");
            p.setupIE();

            Console.WriteLine("Up And Running!");
            p.closed.WaitOne();

            Console.WriteLine("Shutting down ...");
            System.Threading.Thread.Sleep(2000);
        }

    }
}
+3
source share

All Articles