为什么当我调试BHO pro时,我按** F12 **然后按** F5 **时,IE网页将崩溃,

时间:2019-05-10 03:09:36

标签: c# internet-explorer bho

我创建了一个here之类的工作Internet Explorer 11 BHO库,当我尝试在BHO类中使用UdpClient.BeginReceive时,按 F12 后,网页将崩溃 F5

我认为原因可能是在正确的时间未将其称为 Close 方法。 因此,我尝试在SetSite和析构函数方法中关闭udp客户端。

设置并获取网站代码:

        int IObjectWithSite.SetSite(object site)
        {
            if (site != null)
            {
                var serviceProv = (IServiceProvider)site;
                var guidIWebBrowserApp = Marshal.GenerateGuidForType(typeof(IWebBrowserApp)); // new Guid("0002DF05-0000-0000-C000-000000000046");
                var guidIWebBrowser2 = Marshal.GenerateGuidForType(typeof(IWebBrowser2)); // new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E");
                IntPtr intPtr;
                serviceProv.QueryService(ref guidIWebBrowserApp, ref guidIWebBrowser2, out intPtr);

                webBrowser = (IWebBrowser2)Marshal.GetObjectForIUnknown(intPtr);

                ((DWebBrowserEvents2_Event)webBrowser).BeforeScriptExecute += S2_BeforeScriptExecute;
                tryInitClient();
            }
            else
            {
                ((DWebBrowserEvents2_Event)webBrowser).BeforeScriptExecute -= S2_BeforeScriptExecute;
                webBrowser = null;
                tryDestructClient();
            }
            return 0;
        }
        int IObjectWithSite.GetSite(ref Guid guid, out IntPtr ppvSite)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
            int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
            Marshal.Release(punk);

            return hr;
        }

S2_BeforeScriptExecute代码:

        private void S2_BeforeScriptExecute(object pDispWindow)
        {
            //System.Windows.Forms.MessageBox.Show("it sucks, no name, try id cxl S2_BeforeScriptExecute");
            //if (pDisp != this.site) { return; }
            if(webBrowser != null)
            {
                dynamic window = webBrowser.Document.parentWindow;
                IExpando windowEx = (IExpando)window;
                windowEx.AddProperty("test");
                window.test= this;
            }
        }

方法搜索:

        public void search()
        {
            ......

            tryInitClient();
            udpClient.Send(data_result, data_result.Length, new IPEndPoint(IPAddress.Broadcast, 4942));
        }

tryInitClient:

        private void tryInitClient()
        {
            if (udpClient == null)
            {
                MessageBox.Show("udp will init "+ udpClient);
                this.broadcastAddress = new IPEndPoint(IPAddress.Any, 4942);
                this.udpClient = new UdpClient();
                this.udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                this.udpClient.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.
                this.udpClient.Client.Bind(this.broadcastAddress);
                this.udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
            }
        }

回调:

        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                byte[] receiveBytes = udpClient.EndReceive(ar, ref broadcastAddress);
                this.udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
            }
            catch (ObjectDisposedException ex)
            {
                MessageBox.Show("udp closed");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

但是它不起作用。网页仍然崩溃。 IE_ERROR

当我注释掉BeginReceive行时,网页正常运行。

Close UdpClient is used for abort socket's BeginReceive()

0 个答案:

没有答案