detect if a oleobject windows isn't valid by handle

hidrau
hidrau used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software architect
Top Expert 2012
Commented:
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.

Author

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 architect
Top Expert 2012

Commented:
This should also work - similar to my example. Windows handle must be valid and if quit works - then this is your ie window.

Author

Commented:
Thanks for your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial