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

Connect to a Running Instance of Internet Explorer

I want to be able to connect to all running instances of IE and get their current URL's. There is a Microsoft Knowlege Base article to do just that (How To Connect to a Running Instance of Internet Explorer 176792) but that one is no longer available. There is a Java example, but I'm a C/C++ programmer and can't quite understand it.

I'd prefer a code example in C or C++. Win32 api's only please (no MFC!)
0
dddogget
Asked:
dddogget
  • 7
  • 2
1 Solution
 
LedigimateCommented:
If you can convince us you're not developing spyware, you might just get an answer

;)
0
 
LedigimateCommented:
J K
We're working on it...
0
 
dddoggetAuthor Commented:
My app is for a Classroom Management solution. I've written a BHO to so that the teacher can already limit the URL's of the students as she wishes. However, when she first starts limiting, I have no idea where all of the current instances of IE are at. I can expand my DHO dll to do this, but I found a Java snippett of code on the MSDN site which shows how to do this. That Knowlede Base article references an article dor C/C++ programmers, but that is no longer available on the Microsoft site. I'm NOT a com expert (I taught 8080 assembler at the local university many years ago, so I'm much more at home with low-level code...)

Other than that, I guess you can look me up on Experts Exchange and see the types of questions I've been asking through the years. They have ALL been centered arround what I need for the enhancement of a Classroom Management app.
Other than that
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LedigimateCommented:
This should be easy if all running instances has your BHO attached.
If I understand you correctly, you want to be able to get the current URL's of the running instances that have been created WITHOUT your BHO attached, right?
0
 
dddoggetAuthor Commented:
That's correct. I have a BHO running (unless the school has a Group Policy which disallows BHO's). However, I want to see how to do this WITHOUT a BHO. MSDN has an example of how to do this with Java and it has a "link" to a C++ example, however that link is no longer valid.
0
 
LedigimateCommented:
The attached c++ code extracts the URLs in all running instances of Internet Explorer to an array without the use of BHOs.
It works in Windows XP.
It also uses the COM library, so use it somewhere after CoInitialize();
If you only need the number of URLs, then you don't need to use an array.
The URLCount variable is used to count the URLs.
If you can test it in Windows Vista, please let me know if it works in Windows Vista.

bool GetURLs(void) {
	HRESULT hr;
	SHDocVw::IShellWindowsPtr spSHWinds;
	hr = spSHWinds.CreateInstance (__uuidof(SHDocVw::ShellWindows));
	if (FAILED (hr))
		return false;
	long nCount = spSHWinds->GetCount ();
	IDispatchPtr spDisp;
	BSTR URLs[50]; // assuming the maximum number of open URLs in IE is 50
	int URLCount = 0;
	for (long i = 0; i < nCount; i++) // iterate through shell windows:
	{
		_variant_t va (i, VT_I4);
		spDisp = spSHWinds->Item (va);
		IWebBrowser2 * pWebBrowser = NULL;
		hr = spDisp.QueryInterface (IID_IWebBrowser2, &pWebBrowser);
		if (pWebBrowser != NULL) // if it's an IE browser instance
		{
			HRESULT hr;
			// Retrieve the URL.
			hr = pWebBrowser->get_LocationURL(&URLs[URLCount]);
			if (SUCCEEDED (hr))
				URLCount++;
		}
		pWebBrowser->Release ();
	}
	return false;
}

Open in new window

0
 
LedigimateCommented:
I forgot to mention, is uses the shdocvw.dll COM component, so you have to import it using
#import "shdocvw.dll"
0
 
LedigimateCommented:
I apologize.  The previous code didn't filter out the URLs from instances of Windows Eplorer (Explorer.exe).  The below code has been corrected to retrieve and count only the URLs from instances of Internet Explorer (IEXPLORE.EXE).

bool GetURLs(void) {
	HRESULT hr;
	SHDocVw::IShellWindowsPtr spSHWinds;
	hr = spSHWinds.CreateInstance (__uuidof(SHDocVw::ShellWindows));
	if (FAILED (hr))
		return false;
	long nCount = spSHWinds->GetCount ();
	IDispatchPtr spDisp;
	BSTR URLs[50]; // assuming the maximum number of open URLs in IE is 50
	BSTR bsFName;
	wchar_t *wctFName;
	int URLCount = 0;
	for (long i = 0; i < nCount; i++) // iterate through shell windows:
	{
		_variant_t va (i, VT_I4);
		spDisp = spSHWinds->Item (va);
		IWebBrowser2 * pWebBrowser = NULL;
		hr = spDisp.QueryInterface (IID_IWebBrowser2, &pWebBrowser);
		if (pWebBrowser != NULL) 
		{
			hr = pWebBrowser->get_FullName(&bsFName);
			wctFName = bsFName;
			if (wcsstr(_wcsupr(wctFName), L"\\IEXPLORE.EXE") != NULL) // if it's an IE browser instance
			{
				hr = pWebBrowser->get_LocationURL(&URLs[URLCount]); // Retrieve the URL
				if (SUCCEEDED (hr))
				{
					URLCount++;
					// do here what you need to do with the URL
				}
			}
		}
		pWebBrowser->Release ();
	}
	if (URLCount = 0) // There were running instances of IE
		return false;
	return true; // There weren't running instances of IE
}

Open in new window

0
 
LedigimateCommented:
ehemm...
swap the comments on line 35 and 37 of above code.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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