Problems with IWebBrowser2

Hello,

i am developing one application, where i have to use IWebBrowser2. And now i have got problems programm just crashes, when i am trying to append anything. I have tried to add madExcept, but it still crashes without any error message.
procedure WebBrowserAppendHTML(doc: IHTMLDocument2; const html: WideString);
var
   Range: IHTMLTxtRange;
   body: IHTMLBodyElement;
begin
 
   //doc.QueryInterface(IHTMLBodyElement, body);
 
   MessageBox(0, '3', 'Append HTML', $00);
   body := doc.body as IHTMLBodyElement;
   MessageBox(0, '4', 'Append HTML', $00);
   Range := body.createTextRange as IHTMLTxtRange; // CRASHES ON THIS LINE
   MessageBox(0, '5', 'Append HTML', $00);
 
   Range.Collapse(False) ;
   MessageBox(0, '6', 'Append HTML', $00);
   Range.PasteHTML(html) ;
   MessageBox(0, '7', 'Append HTML', $00);
end;
 
procedure WebBrowserAppendHTML(wb: IWebBrowser2; const html: WideString); // This function is being called
var
  doc: IHTMLDocument2;
begin
   //wb.QueryInterface(IHTMLDocument2, doc);
   doc := wb.Document as IHTMLDocument2;
   WebBrowserAppendHTML(doc, html);
   MessageBox(0, 'END', 'Append HTML', $00);
end;

Open in new window

LVL 4
4e4enAsked:
Who is Participating?
 
2266180Connect With a Mentor Commented:
in BeforeNavigate you do whatever operations you need to do BEFORE you send anything to the target server.
in NavigateComplete you do whatever operations you need to do AFTER everything has been sent and received.

so it all depends on what you need to do. I don't know what you want to do so I cannot tell you where is the right place to do it :) Give me some more info on what you need to do and I will give you some more explanations.
0
 
2266180Commented:
you probably did not enable madexcept, though there can be some crashes that just cannot be caught. In any case, since you have found the line with error, madexcept won't help you any further since you need to know why the error is happening.
btw, does your crash come with some error message?

back to your problem: did you make sure that the document is loaded before calling webbrowserappendhtml?

one simple check is to modify that procedure to be:

note: I'm going to sleep now, but I'll get back to you in the morning.
procedure WebBrowserAppendHTML(wb: IWebBrowser2; const html: WideString); // This function is being called
var
  doc: IHTMLDocument2;
begin
  if wb.ReadyState<READYSTATE_INTERACTIVE then
  begin
    showmessage('document not loaded yet.');
    exit;
  end;
   //wb.QueryInterface(IHTMLDocument2, doc);
   doc := wb.Document as IHTMLDocument2;
   WebBrowserAppendHTML(doc, html);
   MessageBox(0, 'END', 'Append HTML', $00);
end;

Open in new window

0
 
4e4enAuthor Commented:
Thanks, now it is partialy working.

One more question, why this code causes same crashing, but previous method doesn't help?

function WebBrowserGetCookies(doc: IHTMLDocument2): string;
begin
  Result := '';
  if Assigned(doc) then
    Result := doc.cookie;
end;
 
function WebBrowserGetCookies(wb: IWebBrowser2): string;
begin
  //
  Result := WebBrowserGetCookies(wb.Document as IHTMLDocument2);
end;

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
2266180Commented:
"some" crashing means that only in some cases crashes. probably in those when there is no cookie? :)
0
 
4e4enAuthor Commented:
Always. i am calling it from BeforeNavigate function. That may be a reason for crashings.

0
 
2266180Commented:
of course. beforenavigate means that the page has not been loaded yet. use onnavigatecomplete event instead
0
 
4e4enAuthor Commented:
Is it possible onnavigatecomplete event parse PostData, Headers?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.