Solved

Control a running IE4.0 application with COM

Posted on 1998-01-17
7
605 Views
Last Modified: 2013-12-03
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
Comment
Question by:jhance
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:galkin
ID: 1410117
I lloket at ROT viewer(Irotview.exe file) utility that is shipped with VC++ IE4 doesn't register itself in ROT!
0
 
LVL 32

Author Comment

by:jhance
ID: 1410118
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
 
LVL 32

Author Comment

by:jhance
ID: 1410119
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:galkin
ID: 1410120
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
 
LVL 32

Author Comment

by:jhance
ID: 1410121
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
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1410122
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
 
LVL 23

Expert Comment

by:chensu
ID: 1410123
> 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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now