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();
}
LVL 32
jhanceAsked:
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.

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
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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

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
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
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
Microsoft Development

From novice to tech pro — start learning today.