detect if a oleobject windows isn't valid by handle

Hello,

I have this function where I open my internet explorer with a url. I need to detect if my internet explorer was closed and then allow my function create a new call, if it is not closed, I just send the new url to it. How can I check this once I have the handle kept?

function OpenIE(url: string; ie: IWebBrowser2): IWebBrowser2;
var

  Flags, TargetFrameName, PostData, Headers: OleVariant;
begin
  if Not Assigned(ie) then
  Begin
    // Uses ComObj + SHDocVw_TLB
    ie := CreateOleObject('InternetExplorer.Application') as IWebBrowser2;
    //  SetParent(ie.HWND, form1.Handle);
    ie.Left := (Screen.Width - 750) div 2;
    ie.Top := (Screen.Height - 560) div 2;


    ie.Width := 750;
    ie.Height := 560;
    ie.MenuBar := false;
    ie.AddressBar := false;
    ie.Resizable := True;
    ie.StatusBar := false;
    ie.ToolBar := 0;

    ie.Navigate2(Url,Flags,TargetFrameName,PostData,Headers);
    ie.Visible := true;
  End Else
  Begin
    ie.Navigate2(Url,Flags,TargetFrameName,PostData,Headers);
  End;

  result := ie;
//  http://codeverge.com/embarcadero.delphi.win32/opening-an-application-inside-delphi/1049698
end;

Open in new window


Thanks
Alex
LVL 1
hidrauAsked:
Who is Participating?
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.

Sinisa VukSoftware architectCommented:
maybe you can check with such function:
function IsIEActive(obj: IDispatch): Boolean;
var
  ClassID: TCLSID;
  Unknown: IUnknown;
begin
  Result := False;
  try
    if Assigned(obj) then
    begin
      Unknown := (IWebBrowser2(obj).Document as IHTMLDocument2);
      Result := True;
    end;
  except
  end;
end;

if Not IsIEActive(ie) then
  Begin
    // Uses ComObj + SHDocVw_TLB
    ie := CreateOleObject('InternetExplorer.Application'); 
...

Open in new window


...my function will raise exception but it is handled... I just want to get IDocument2 interface from valid ie object.

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
hidrauAuthor Commented:
hummm,

but before you post your example, I have try this, what do you also think?

{  if Assigned(xie) then
  Begin

    try
      if IsWindow(xie.HWND) then
      Begin
        xie.quit;
        xie := nil;
      End;
    Except
       xie := nil
    end;
  End;}

  xie := OpenIE(Edit1.Text, xie);

Open in new window


xie is a private variable from my main form and when I create the obj I give to it the created obj, then I test it with iswindow.
Sinisa VukSoftware architectCommented:
This should also work - similar to my example. Windows handle must be valid and if quit works - then this is your ie window.
hidrauAuthor Commented:
Thanks for your help
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.