Solved

Control a running IE4.0 application with COM

Posted on 1998-01-17
7
609 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Method 'OpenReport' of the object Docmd failed 7 30
DataAdapter, stored procedures and parameters 2 48
Problem to DocumentFormat 8 92
Recommendation vb6 to vb.net or others 14 148
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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