[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1000
  • Last Modified:

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

0
4e4en
Asked:
4e4en
  • 4
  • 3
1 Solution
 
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
 
2266180Commented:
"some" crashing means that only in some cases crashes. probably in those when there is no cookie? :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
2266180Commented:
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now