实现由 WebBrowser 控件调用的 IDispatch::Invoke

发布于 2024-12-07 06:09:15 字数 1312 浏览 4 评论 0原文

我正在尝试执行他们在这篇文章中解释的操作在控制下载和执行部分。据我了解,该上下文中的 Web 浏览器控件 不是 .NET 的 WebBrowser

我想做的是控制 WebBrowser 控件下载的内容。我一直在寻找这个,并且总是在 csEXWB 中找到,其中有一个我无法破译的巨大代码。

到目前为止我所做的是继承.NET的WebBrowser,通过使用[ComVisible(true)]属性使我继承的类com可见,将此方法添加到我的类中(取自 csEXWB):

    [DispId(HTMLDispIDs.DISPID_AMBIENT_DLCONTROL)]
    public int Idispatch_AmbiantDlControl_Invoke_Handler()
    {
        return (int)m_DLCtlFlags;
    }

然后调用这行代码,其中 Browser 是我的派生类的实例:

IfacesEnumsStructsClasses.IOleControl oleControl = Browser.ActiveXInstance as IfacesEnumsStructsClasses.IOleControl;
oleControl.OnAmbientPropertyChange(IfacesEnumsStructsClasses.HTMLDispIDs.DISPID_AMBIENT_DLCONTROL);

所以我希望 oleControl 会调用我的Idispatch_AmbiantDlControl_Invoke_Handler 方法,但它没有。我不知道怎么做,这可能是我的代码所缺少的,oleControl 应该知道在哪个对象上调用我的 Idispatch_AmbiantDlControl_Invoke_Handler 方法。

我上面链接的文章说的是它将调用您的 IDispatch::Invoke你的是什么意思。我如何告诉 oleControl 哪个对象是我的 IDispatch。希望我说得有道理。

I'm trying to do what they explain on this article in the Controlling Download and Execution section. I understand the Web Browser Control in that context is not .NET's WebBrowser.

What I'm trying to do is to gain control over what the WebBrowser control downloads. I've been searching for this a lot and always en up in the csEXWB, which has a huge code that I just cannot decipher.

What I've done so far is inherit .NET's WebBrowser, make my inherited class com visible by using the [ComVisible(true)] attribute, add this method to my class (taken from csEXWB):

    [DispId(HTMLDispIDs.DISPID_AMBIENT_DLCONTROL)]
    public int Idispatch_AmbiantDlControl_Invoke_Handler()
    {
        return (int)m_DLCtlFlags;
    }

And then call this line of code, where Browser is an instance of my derived class:

IfacesEnumsStructsClasses.IOleControl oleControl = Browser.ActiveXInstance as IfacesEnumsStructsClasses.IOleControl;
oleControl.OnAmbientPropertyChange(IfacesEnumsStructsClasses.HTMLDispIDs.DISPID_AMBIENT_DLCONTROL);

So what I'm hoping is that the oleControl will call my Idispatch_AmbiantDlControl_Invoke_Handler method, which it doesn't. I don't know how, and this is probably what my code is missing, is the oleControl supposed to know on which object to call my Idispatch_AmbiantDlControl_Invoke_Handler method.

What the article I linked above says is it will call your IDispatch::Invoke. What does it mean by your. How do I tell oleControl which object is my IDispatch. Hope I'm making any sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-12-14 06:09:15

这是一个定制的 Web 浏览器,允许您更改 DLCONTROL 标志。

这是示例代码:

public partial class Form1 : Form
{
    private MyWebBrowser _webBrowser;

    public Form1()
    {
        InitializeComponent();

        _webBrowser = new MyWebBrowser();
        _webBrowser.Dock = DockStyle.Fill;

        Controls.Add(_webBrowser);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _webBrowser.DownloadControlFlags = (int)WebBrowserDownloadControlFlags.DOWNLOADONLY;
        _webBrowser.Navigate("http://mysamplewebsite");
    }
}

以及定制的 WebBrowser 代码:

public class MyWebBrowser : WebBrowser
{
    private const int DISPID_AMBIENT_DLCONTROL = -5512;
    private int _downloadControlFlags;

    // we want our site class, not the default one
    protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
    {
        return new MyWebBrowserSite(this);
    }

    [DispId(DISPID_AMBIENT_DLCONTROL)]
    public int DownloadControlFlags
    {
        get
        {
            return _downloadControlFlags;
        }
        set
        {
            if (_downloadControlFlags == value)
                return;

            _downloadControlFlags = value;
            IOleControl ctl = (IOleControl)ActiveXInstance;
            ctl.OnAmbientPropertyChange(DISPID_AMBIENT_DLCONTROL);
        }
    }

    protected class MyWebBrowserSite : WebBrowserSite, IReflect
    {
        private Dictionary<int, PropertyInfo> _dispidCache;
        private MyWebBrowser _host;

        public MyWebBrowserSite(MyWebBrowser host)
            : base(host)
        {
            _host = host;
        }

        object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
        {
            object ret = null;
            // Check direct IDispatch call using a dispid (see http://msdn.microsoft.com/en-us/library/de3dhzwy.aspx)
            const string dispidToken = "[DISPID=";
            if (name.StartsWith(dispidToken))
            {
                int dispid = int.Parse(name.Substring(dispidToken.Length, name.Length - dispidToken.Length - 1));
                if (_dispidCache == null)
                {
                    // WebBrowser has many properties, so we build a dispid cache on it
                    _dispidCache = new Dictionary<int, PropertyInfo>();
                    foreach (PropertyInfo pi in _host.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                    {
                        if ((!pi.CanRead) || (pi.GetIndexParameters().Length > 0))
                            continue;

                        object[] atts = pi.GetCustomAttributes(typeof(DispIdAttribute), true);
                        if ((atts != null) && (atts.Length > 0))
                        {
                            DispIdAttribute da = (DispIdAttribute)atts[0];
                            _dispidCache[da.Value] = pi;
                        }
                    }
                }

                PropertyInfo property;
                if (_dispidCache.TryGetValue(dispid, out property))
                {
                    ret = property.GetValue(_host, null);
                }
            }
            return ret;
        }

        FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
        {
            return GetType().GetFields(bindingAttr);
        }

        MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
        {
            return GetType().GetMethods(bindingAttr);
        }

        PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
        {
            return GetType().GetProperties(bindingAttr);
        }

        FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
        {
            throw new NotImplementedException();
        }

        PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
        {
            throw new NotImplementedException();
        }

        PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        Type IReflect.UnderlyingSystemType
        {
            get { throw new NotImplementedException(); }
        }
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("B196B288-BAB4-101A-B69C-00AA00341D07")]
    internal interface IOleControl
    {
        void Reserved0();
        void Reserved1();
        void OnAmbientPropertyChange(int dispID);
        void Reserved2();
    }
}

[Flags]
public enum WebBrowserDownloadControlFlags: uint
{
    DLIMAGES                          = 0x00000010,
    VIDEOS                            = 0x00000020,
    BGSOUNDS                          = 0x00000040,
    NO_SCRIPTS                        = 0x00000080,
    NO_JAVA                           = 0x00000100,
    NO_RUNACTIVEXCTLS                 = 0x00000200,
    NO_DLACTIVEXCTLS                  = 0x00000400,
    DOWNLOADONLY                      = 0x00000800,
    NO_FRAMEDOWNLOAD                  = 0x00001000,
    RESYNCHRONIZE                     = 0x00002000,
    PRAGMA_NO_CACHE                   = 0x00004000,
    NO_BEHAVIORS                      = 0x00008000,
    NO_METACHARSET                    = 0x00010000,
    URL_ENCODING_DISABLE_UTF8         = 0x00020000,
    URL_ENCODING_ENABLE_UTF8          = 0x00040000,
    NOFRAMES                          = 0x00080000,
    FORCEOFFLINE                      = 0x10000000,
    NO_CLIENTPULL                     = 0x20000000,
    SILENT                            = 0x40000000,
    OFFLINEIFNOTCONNECTED             = 0x80000000,
    OFFLINE                           = OFFLINEIFNOTCONNECTED,
}

Here is a customized WebBrowser that allows you to change the DLCONTROL flags.

This is an example code:

public partial class Form1 : Form
{
    private MyWebBrowser _webBrowser;

    public Form1()
    {
        InitializeComponent();

        _webBrowser = new MyWebBrowser();
        _webBrowser.Dock = DockStyle.Fill;

        Controls.Add(_webBrowser);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _webBrowser.DownloadControlFlags = (int)WebBrowserDownloadControlFlags.DOWNLOADONLY;
        _webBrowser.Navigate("http://mysamplewebsite");
    }
}

And the customized WebBrowser code:

public class MyWebBrowser : WebBrowser
{
    private const int DISPID_AMBIENT_DLCONTROL = -5512;
    private int _downloadControlFlags;

    // we want our site class, not the default one
    protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
    {
        return new MyWebBrowserSite(this);
    }

    [DispId(DISPID_AMBIENT_DLCONTROL)]
    public int DownloadControlFlags
    {
        get
        {
            return _downloadControlFlags;
        }
        set
        {
            if (_downloadControlFlags == value)
                return;

            _downloadControlFlags = value;
            IOleControl ctl = (IOleControl)ActiveXInstance;
            ctl.OnAmbientPropertyChange(DISPID_AMBIENT_DLCONTROL);
        }
    }

    protected class MyWebBrowserSite : WebBrowserSite, IReflect
    {
        private Dictionary<int, PropertyInfo> _dispidCache;
        private MyWebBrowser _host;

        public MyWebBrowserSite(MyWebBrowser host)
            : base(host)
        {
            _host = host;
        }

        object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
        {
            object ret = null;
            // Check direct IDispatch call using a dispid (see http://msdn.microsoft.com/en-us/library/de3dhzwy.aspx)
            const string dispidToken = "[DISPID=";
            if (name.StartsWith(dispidToken))
            {
                int dispid = int.Parse(name.Substring(dispidToken.Length, name.Length - dispidToken.Length - 1));
                if (_dispidCache == null)
                {
                    // WebBrowser has many properties, so we build a dispid cache on it
                    _dispidCache = new Dictionary<int, PropertyInfo>();
                    foreach (PropertyInfo pi in _host.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                    {
                        if ((!pi.CanRead) || (pi.GetIndexParameters().Length > 0))
                            continue;

                        object[] atts = pi.GetCustomAttributes(typeof(DispIdAttribute), true);
                        if ((atts != null) && (atts.Length > 0))
                        {
                            DispIdAttribute da = (DispIdAttribute)atts[0];
                            _dispidCache[da.Value] = pi;
                        }
                    }
                }

                PropertyInfo property;
                if (_dispidCache.TryGetValue(dispid, out property))
                {
                    ret = property.GetValue(_host, null);
                }
            }
            return ret;
        }

        FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
        {
            return GetType().GetFields(bindingAttr);
        }

        MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
        {
            return GetType().GetMethods(bindingAttr);
        }

        PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
        {
            return GetType().GetProperties(bindingAttr);
        }

        FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
        {
            throw new NotImplementedException();
        }

        PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
        {
            throw new NotImplementedException();
        }

        PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
        {
            throw new NotImplementedException();
        }

        Type IReflect.UnderlyingSystemType
        {
            get { throw new NotImplementedException(); }
        }
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("B196B288-BAB4-101A-B69C-00AA00341D07")]
    internal interface IOleControl
    {
        void Reserved0();
        void Reserved1();
        void OnAmbientPropertyChange(int dispID);
        void Reserved2();
    }
}

[Flags]
public enum WebBrowserDownloadControlFlags: uint
{
    DLIMAGES                          = 0x00000010,
    VIDEOS                            = 0x00000020,
    BGSOUNDS                          = 0x00000040,
    NO_SCRIPTS                        = 0x00000080,
    NO_JAVA                           = 0x00000100,
    NO_RUNACTIVEXCTLS                 = 0x00000200,
    NO_DLACTIVEXCTLS                  = 0x00000400,
    DOWNLOADONLY                      = 0x00000800,
    NO_FRAMEDOWNLOAD                  = 0x00001000,
    RESYNCHRONIZE                     = 0x00002000,
    PRAGMA_NO_CACHE                   = 0x00004000,
    NO_BEHAVIORS                      = 0x00008000,
    NO_METACHARSET                    = 0x00010000,
    URL_ENCODING_DISABLE_UTF8         = 0x00020000,
    URL_ENCODING_ENABLE_UTF8          = 0x00040000,
    NOFRAMES                          = 0x00080000,
    FORCEOFFLINE                      = 0x10000000,
    NO_CLIENTPULL                     = 0x20000000,
    SILENT                            = 0x40000000,
    OFFLINEIFNOTCONNECTED             = 0x80000000,
    OFFLINE                           = OFFLINEIFNOTCONNECTED,
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文