{"id":2614,"date":"2022-08-30T15:26:20","date_gmt":"2022-08-30T15:26:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/twebbrowser-crashes-with-embedded-youtube-clips-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:20","modified_gmt":"2022-08-30T15:26:20","slug":"twebbrowser-crashes-with-embedded-youtube-clips-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/twebbrowser-crashes-with-embedded-youtube-clips-collection-of-common-programming-errors\/","title":{"rendered":"TWebBrowser crashes with embedded Youtube clips-Collection of common programming errors"},"content":{"rendered":"<p>Here is my code:<\/p>\n<pre><code>type\n  TForm1 = class(TForm)\n    WebBrowser1: TWebBrowser;\n    Button1: TButton;\n    Button2: TButton;\n    procedure Button1Click(Sender: TObject);\n    procedure Button2Click(Sender: TObject);\n  end;\n\nimplementation\nuses ActiveX;\n\nprocedure TForm1.Button1Click(Sender: TObject); \/\/ method 1\nvar\n  HtmlFile: string;\nbegin\n  HtmlFile := ExtractFilePath(Application.ExeName) + 'test.html';\n  WebBrowser1.Navigate(HtmlFile);\nend;\n\nprocedure LoadHtml(wb: TWebBrowser; HTMLStr: string);\nvar\n  aStream: TMemoryStream;\nbegin\n  wb.Navigate('about:blank'); \/\/ reset the webbrowser\n  while wb.ReadyState &lt; READYSTATE_INTERACTIVE do \/\/ wait to load the empty page\n    Application.ProcessMessages;\n  if Assigned(wb.Document) then\n  begin\n    aStream := TMemoryStream.Create;\n    try\n      aStream.WriteBuffer(Pointer(HTMLStr)^, Length(HTMLStr));\n      aStream.Seek(0, soFromBeginning);\n      (wb.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));\n    finally\n      aStream.Free;\n    end;\n  end;\nend;\n\nprocedure TForm1.Button2Click(Sender: TObject); \/\/ method 2\nbegin\n  LoadHtml(WebBrowser1,\n    ''+\n    '   '+\n    '   '+\n    '   '+\n    '   '+\n    '   '+\n    '   '+\n    ''\n    );\nend;\n<\/code><\/pre>\n<p><strong>test.html<\/strong><\/p>\n<pre><code>\n\n\n\n\n\n<\/code><\/pre>\n<p>My application crashes in both methods. I get An unhandled win32 exception (caused by Flash player <code>Exception EInvalidOp in module Flash10u.ocx at 00108657. Invalid floating point operation<\/code>).<\/p>\n<ul>\n<li>I tried this code on D5, D7, D9.<\/li>\n<li>I tried to re-import SHDocVw.dll.<\/li>\n<li>I also tried to use EmbeddedWB control instead of TWebBroser&#8230;<\/li>\n<li>Internet Explorer\/Avant\/Maxthon has no problems with this HTML (all based on IE ActiveX).<\/li>\n<\/ul>\n<p>Any suggestions or a fix?<\/p>\n<p>How can I catch this error or even suppress it?<\/p>\n<p>Is there a way to manipulate or change the HTML on the fly via a TWebBrowser event, so I can display an Image instead of the Flash player, same as Ad-Blockers works? (My customers have that code in their sites over the internet, and my Delphi application provides a fast preview)<\/p>\n<p><em><strong>UPDATE<\/strong><\/em><\/p>\n<p>I used a TTimer to enable\/disable FPU (based on Arjen&#8217;s idea):<\/p>\n<pre><code>function Get8087CW: Word; \/\/ for D5\nasm\n        PUSH    0\n        FNSTCW  [ESP].Word\n        POP     EAX\nend;\n\nprocedure TForm1.FormCreate(Sender: TObject);\nbegin\n  Timer1.Enabled := False;\n  Timer1.Interval := 5000; \/\/ 5 sec\n  Saved8087CW := Get8087CW;\nend;\n\nprocedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject;\n  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,\n  Headers: OleVariant; var Cancel: WordBool);\nbegin\n  Timer1.Enabled := False;\n  System.Set8087CW($133F); \/\/ Disable all fpu exceptions\nend;\n\nprocedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;\n  const pDisp: IDispatch; var URL: OleVariant);\nbegin\n   Timer1.Enabled := True;\nend;\n\nprocedure TForm1.Timer1Timer(Sender: TObject);\nbegin\n  Timer1.Enabled := False;\n  Set8087CW(Saved8087CW);\nend;\n<\/code><\/pre>\n<p><em><strong>UPDATE (2)<\/strong><\/em><\/p>\n<p>I ended up masking the FPU exception in the application start-up. there was no (known) impact on my application ever since.<\/p>\n<ol>\n<li>\n<p>Try to disable temporarily FPU exception with Set8087CW(0x133f); info<\/p>\n<\/li>\n<li>\n<p>A bit more beautiful solution:<\/p>\n<pre><code>Math.SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow,\n  exUnderflow, exPrecision]);\n<\/code><\/pre>\n<p>If I read documentation correct, <code>Math.SetExceptionMask<\/code> silences every exception mentioned.<\/p>\n<p>However, it is just cleaner and more beautiful version of @Arjen&#8217;s approach.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-04 09:37:54. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Here is my code: type TForm1 = class(TForm) WebBrowser1: TWebBrowser; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end; implementation uses ActiveX; procedure TForm1.Button1Click(Sender: TObject); \/\/ method 1 var HtmlFile: string; begin HtmlFile := ExtractFilePath(Application.ExeName) + &#8216;test.html&#8217;; WebBrowser1.Navigate(HtmlFile); end; procedure LoadHtml(wb: TWebBrowser; HTMLStr: string); var aStream: TMemoryStream; begin wb.Navigate(&#8216;about:blank&#8217;); \/\/ reset the webbrowser [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2614","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2614"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2614\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}