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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.