RE: Webbrowser / IE FileDownload Event Question

From: Shelley (Shelley_at_discussions.microsoft.com)
Date: 07/28/04


Date: Wed, 28 Jul 2004 07:14:08 -0700

I am going to answer my own question here. I got the DocumentComplete function working. I am still having problems though, because I also need the IHtmlDocument2 document, which I could get from the Microsoft WebBrowser component. How do I get access to this?

full source:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Data;
using System.IO;

namespace HostingActiveX
{

    public class Form1 : System.Windows.Forms.Form
    {

        private System.ComponentModel.Container components = null;
        private MyWebBrowser webBrowser;

        public Form1()
        {

            InitializeComponent();

            webBrowser = new MyWebBrowser();

            webBrowser.Dock = DockStyle.Fill;

            webBrowser.FileDownload += new FileDownloadEventHandler(OnFileDownload);
            webBrowser.DocumentComplete += new DocumentCompleteEventHandler (OnDocumentComplete);
            
            Controls.Add(webBrowser);
        }

        protected void OnFileDownload(object sender, FileDownloadEventArgs e)
        {
            MessageBox.Show("User is trying to download file");
        }

        protected void OnDocumentComplete (object sender, DocumentCompleteEventArgs e)
        {
            MessageBox.Show ("Document completed");
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        private void InitializeComponent()
        {

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
        }

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            webBrowser.Url = "http://www.microsoft.com";
        }
    }

    public class MyWebBrowser : AxHost, IMyWebBrowserEvents
    {

        private IMyWebBrowser control;
        private ConnectionPointCookie cookie;

        protected string url;
       
        public event FileDownloadEventHandler FileDownload;
        public event DocumentCompleteEventHandler DocumentComplete;

        public MyWebBrowser() : base("8856f961-340a-11d0-a96b-00c04fd705a2")
        {
        }

        public void RaiseFileDownload(ref bool cancel, bool load)
        {
            FileDownloadEventArgs e = new FileDownloadEventArgs(cancel, load);
            if(FileDownload != null)
                FileDownload(this, e);
            cancel = e.Cancel;
        }

        public void RaiseDocumentComplete([MarshalAs(UnmanagedType.IDispatch)] object pIDisp,
            ref object url)
        {
            DocumentCompleteEventArgs e = new DocumentCompleteEventArgs (pIDisp, url);
            if (DocumentComplete != null)
                DocumentComplete (this, e);
            pIDisp = e.PIDisp;
            url = e.Url;
        }

        protected override void CreateSink()
        {
            try
            {
                cookie = new ConnectionPointCookie(control, this,
                    typeof(IMyWebBrowserEvents)); }
            catch { }
        }

        protected override void DetachSink()
        {
            try
            {
                cookie.Disconnect();
            }
            catch { }
        }

        protected override void AttachInterfaces()
        {
            try
            {
                control = (IMyWebBrowser) GetOcx();
            }
            catch { }
        }

        public string Url
        {
            get { return url; }
            set
            {
                url = value;
                object o = null;
                control.Navigate(url, ref o, ref o, ref o, ref o);
            }
        }
    }

    [Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IMyWebBrowserEvents
    {
        [DispId(270)]
        void RaiseFileDownload(ref bool Cancel, bool Load);

        [DispId(259)]
        void RaiseDocumentComplete ([MarshalAs(UnmanagedType.IDispatch)] object pIDisp,
            ref object url);
    }

    public delegate void FileDownloadEventHandler(object sender, FileDownloadEventArgs e);
    public class FileDownloadEventArgs
    {
        public FileDownloadEventArgs(bool cancel, bool load)
        {
            this.cancel = cancel;
            this.load = load;
        }

        protected bool cancel;
        protected bool load;

        public bool Load
        {
            get{return load;}
        }

        public bool Cancel
        {
            get{return cancel;}
            set{cancel = value;}
        }
    }

    public delegate void DocumentCompleteEventHandler (object sender, DocumentCompleteEventArgs e);
    public class DocumentCompleteEventArgs
    {
        public DocumentCompleteEventArgs ([MarshalAs(UnmanagedType.IDispatch)] object pIDisp,
            object url)
        {
            this.pIDisp = pIDisp;
            this.url = url;
        }

        protected object pIDisp;
        protected object url;

        public object PIDisp
        {
            get{return pIDisp;}
            set{pIDisp = value;}
        }

        public object Url
        {
            get{return url;}
            set{url = value;}
        }
    }

    [Guid("eab22ac1-30c1-11cf-a7eb-0000c05bae0b")]
    interface IMyWebBrowser
    {
        void GoBack();
        void GoForward();
        void GoHome();
        void GoSearch();
        void Navigate(string url, ref object flags, ref object targetFrame, ref
            object postData, ref object headers);
        void Refresh();
        void Refresh2();
        void Stop();
        void GetApplication();
        void GetParent();
        void GetContainer();
    }
}

"Shelley" wrote:

> Did you check out this KB article: http://support.microsoft.com/?kbid=325204
>
> It basically says that, yes there is a bug, and here's a workaround. My only problem with the solution is that it only gives you the FileDownload event. I also need the DownloadComplete event which uses an IDispatch pointer and VARIANT pointer as parameters. I am having problems finding the class corresponding to those, so that I can override the DownloadComplete parameter.
>
> If you have any suggestions, please let me know.
>
> "John M" wrote:
>
> > Hi all,
> >
> > I am attempting to handle the FileDownload, however the event is *never* fired.
> > I have seen the KB article that says this is caused by threading model issues but even when attempting to trigger the event in a compiled (apartment threaded) ActiveX DLL the event doesn't fire.
> >
> > I am developing a BrowserHelperObject in VB. I have successfully hooked into the instanciating instance of IE and I am able to intercept other events with no problems.
> >
> > I am wondering if my problem stems from the Event decleration. When I select the event in VB, I get a stub of:
> >
> > Private Sub <object>_FileDownload(Cancel As Boolean)
> >
> > However, the event is listed on the MSDN with a stub of:
> > Private Sub object_FileDownload( ByRef bCancel As Boolean, ByRef bCancel As Boolean)
> >
> >
> > Can anyone shed any light on what I might be doing wrong or why my stub is out of date ?
> >
> > I have IE6 installed and VisualStudio SP6.
> >
> > Thanks in advance.