3

I want to automate the Internet Explorer using C#. Thus I've written simple Console Application, that creates a new instance of InternetExplorer and then registers on some events.

Following Events work fine: OnQuit, BeforeNavigate2 and NewWindow2

But NewWindow3 does not. Everything compiles and the program can be started, but following exception is thrown when you make a link open in another window: Eine Ausnahme (erste Chance) des Typs "System.ArgumentException" ist in mscorlib.dll aufgetreten.

So what am I doing wrong with that event? I've exactly used the parameters specified in DWebBrowserEvents2_NewWindow3EventHandler

edit: As this seems to be a bug in the Library, is there a possibility to create your own EventHandler Method/Callback stuff? I did some research and have found this page: How do I add an event listener using MSHTML's addEventListener in IE9? Where someone creates a COM-Class with a callback method.

=> How can I extend the InternetExplorer class so that I can access the 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);
        }

    }
}
Community
  • 1
  • 1
pyBlob
  • 41
  • 6
  • It is a bug in the type library, the 3rd argument is actually *int*, not uint. This doesn't get put to the test very often. No, not simple to fix. – Hans Passant Feb 20 '14 at 17:10
  • 1
    that means I can't currently use NewWindow3? When there's no easy fix ... what's the hard one? – pyBlob Feb 20 '14 at 17:52

0 Answers0