• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 617
  • Last Modified:

Control a running IE4.0 application with COM

The following code is trying to find and control a running
Internet Explorer session via COM or, it it can't find one, start a new session.  The problem is that the GetActiveObject() call always fails.  So a new session is always started regardless of how many are already running.

void CIEControlDlg::OnOK()
{
      CString msg;
      HRESULT hresult;
      BSTR bstr;
      VARIANT arg;
      arg.vt = VT_ERROR;
      arg.scode = DISP_E_PARAMNOTFOUND;

      IUnknown *punk = (IUnknown *)NULL;
      
      IWebBrowser2 *iweb = (IWebBrowser2 *)NULL;

      hresult = CoInitialize(NULL);
      CLSID clsid;
      CLSIDFromProgID(L"InternetExplorer.Application.1", &clsid);

      hresult = GetActiveObject(clsid, NULL, &punk);
      if(!SUCCEEDED(hresult)){
            // CoGetClassObject failed - Make a new one
            AfxMessageBox("No IE FOUND", MB_OK);
            hresult = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (LPVOID *)&punk);
            if(!SUCCEEDED(hresult)){
                  AfxMessageBox("CoCreateInstance() failed", MB_OK);
                  CoUninitialize();
                  return;
            }
      }

      hresult = punk->QueryInterface(IID_IWebBrowser2, (LPVOID *)&iweb);
      if(!SUCCEEDED(hresult)){
            AfxMessageBox("QueryInterface() failed", MB_OK);
            punk->Release();
            CoUninitialize();
            return;
      }

      punk->Release();

      iweb->put_Visible((VARIANT_BOOL)TRUE);
      bstr = SysAllocString(OLETEXT("http://www.microsoft.com"));
      hresult = iweb->Navigate(bstr, &arg, &arg, &arg, &arg);
      SysFreeString(bstr);
      if(!SUCCEEDED(hresult)){
            AfxMessageBox("FAILED", MB_OK);
            if(hresult = E_INVALIDARG){
                  AfxMessageBox("E_INVALIDARG", MB_OK);
            }
      }

      //iweb->Quit();
      iweb->Release();
      CoUninitialize();
}
0
jhance
Asked:
jhance
  • 3
  • 2
  • 2
1 Solution
 
galkinCommented:
I lloket at ROT viewer(Irotview.exe file) utility that is shipped with VC++ IE4 doesn't register itself in ROT!
0
 
jhanceAuthor Commented:
This is not an answer to the question.  I've seen this done by other applications and seen references to it in the IE4 documentation.
0
 
jhanceAuthor Commented:
As an example of a program which does this, I give you the OLEVIEW program.  If you start it up and choose the "Automation Objects" and then choose "Internet Explorer", it will connect to a running instance of IE4.0 and show it's COM interfaces.  If you check your running processes before and after doing this, you will see that if a copy of IE is running it will not start another.  If one is not running, it will start one.  I know this program uses CoGetClassObject().
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
galkinCommented:
Internet Explorer is part of shell, it means it is the same application as windows explorer. When shell is running and it is running always and another instance of it is launched the second instance at startup notifies the first instance what action should be done, i.g new windows explore window or new Internet Explorer window should be opened. The first instance does this action and after that the second instance is terminated. Try to launch second instance of windows or Internet explorer and look at process view you will see that ispite of the fact the new window has been opened there is only one instance of explorer.exe(shell exe). The same situation is when you create instance of IE with CoCreateInstance. The new instance of IE is launched with /Embedded or /Automation command line which is passed to the running instance of IE and automation object is created. Then the second insdtance is terminated. That's why IE doesn't register itself in ROT it always has only one instance running.
By the way this sometimes causes problem, if IE crashes entire shell crashes.
0
 
jhanceAuthor Commented:
No, you are incorrect here.  If you have a "generic" instance of IE, the you will see it as Iexplore in the process viewer.  If you think that Windows and IE are "joined at the hip" then you have fallen prey to Microsoft's propaganda.  Internet Explorer and Windows Explorer are very separate and distinct applications each having their own interfaces.

If you have something more of substance to add, please do so but I am not looking to argue about semantics or Microsoft's marketing philosophy here.
0
 
chensuCommented:
I had spent plenty of time on this issue since I asked that 500 points question here. I almost gave it up. Why does Microsoft do it differently? Then I happened to find the answer several days ago. Only Microsoft itself has the answer. Take a look at the KB article Q176792: "HOWTO: Connecting to a Running Instance of Internet Explorer".
0
 
chensuCommented:
> If you check your running processes before and after doing this, you will see that if a copy of IE is running it will not start another. If one is not running, it will start one.

You may override this behavior by selecting the "Browse in a new process" option of the Internet Properties.
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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