Solved

Control a running IE4.0 application with COM

Posted on 1998-01-17
7
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…

738 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