Почему веб-страница IE падает после того, как я нажимаю ** F12 **, а затем нажимаю ** F5 **, когда я отлаживаю свой BHO pro - PullRequest
0 голосов
/ 10 мая 2019

Я создал рабочую библиотеку Internet Explorer 11 BHO, как здесь , когда я пытаюсь использовать UdpClient.BeginReceive в классе BHO, веб-страница падает после того, как я нажму F12 , а затем нажмите F5

Я думаю, что причина может быть в том, что он не вызывается Close метод в нужное время. Поэтому я пытаюсь закрыть клиент udp в SetSite и метод деструктора.

Установить и получить код сайта:

        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 code:

        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 используется для прерывания BeginReceive сокета сокета ()

...