0

I created a Working Internet Explorer 11 BHO libarary like here,When I try to use UdpClient.BeginReceive in BHO class,The web page will crash after I press F12 and then press F5

I think the reason may be that it is not called Close method at the right time. So I try to close udp client in SetSite and the destructor method.

Set and Get Site code:

        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;
            }
        }

method search:

        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);
            }
        }

callback:

        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());
            }
        }

But it doesn't work.The web page still crashed. IE_ERROR

When I comment out the BeginReceive line,The webpage is running normally.

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

C.XL
  • 1
  • 4
  • "...But it doesn't work..." - this information is not really helpful. It does not say what exactly did not work, i.e. did you get any error messages anywhere, did you try to debug the issue to make sure that it actually does the intended thing. – jazb May 10 '19 at 03:11
  • UDP is connectionless, calling udpChannel.Connect merely specifies a default host endpoint for use with the Send method. You do not need to close the client between sends, leaving it open will not leave any connections or listeners running between sends. Let us know if you are getting any exception and inform us the error code. It can help to narrow down the issue. Ref: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.udpclient.close?view=netframework-4.8#System_Net_Sockets_UdpClient_Close – Deepak-MSFT May 10 '19 at 07:58

0 Answers0