Link to home
Start Free TrialLog in
Avatar of pgnatyuk
pgnatyukFlag for Israel

asked on

GetActiveObject and Internet Explorer

Hi All

I want to get the URL from the running IE. I attached a small Win32 app that was supposed to do it. I see  the GetActiveObject returns 0x800401e3 - operation unavailable.

I've read MSDN:
Retrieves a pointer to a running object that has been registered with OLE.

So Internet Explorer is noit registered?
Where is the mistake?

I see few threads on EE with GetActiveObject in the accepted solutions.
I tested the same in an MFC application too. More then it, I found the Edit in IE and I cannot get the URL with GetWindowText too.

It is IE 8. I see the problem on Vista and XP.

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <mshtml.h>
#include <exdisp.h>

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	LPUNKNOWN pUnknown;
	IWebBrowser2* pBrowser;
	HRESULT hr = CoInitialize(NULL);
	if (!SUCCEEDED(hr))
		return FALSE;

	//CLSID clsid;                                
	//CLSIDFromProgID( L"InternetExplorer.Application", &clsid);
	//hr = GetActiveObject(clsid, NULL, &pUnknown);
	hr = GetActiveObject(CLSID_InternetExplorer, NULL, &pUnknown);

	if (SUCCEEDED(hr))
	{
		hr = pUnknown->QueryInterface(IID_IWebBrowser2, (LPVOID*)&pBrowser);
		BSTR str = NULL;
		hr = pBrowser->get_LocationURL(&str);
		SysFreeString(str);
		pBrowser->Release();
		pUnknown->Release();
	}

	CoUninitialize();
	return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of milindsm
milindsm
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pgnatyuk

ASKER

Thank you. Nice article. I knew about it, but never used. I cannot use it now too.
So IE is not registered?
If this is only the answer, I can close this question. I have a bad solution - I got the text from the edit box of the Navigation Bar.
 

HWND FindIEWnd()
{
	HWND hWnd = FindWindow(L"IEFrame", NULL);
	return hWnd;
}

HWND FindURLWnd(HWND hIE)
{
	if (!IsWindow(hIE))
		return NULL;

	HWND hWndURL = NULL;
	HWND hWnd = FindWindowEx(hIE, NULL, L"WorkerW", NULL);
	if (hWnd != NULL)
	{
		hWnd = FindWindowEx(hWnd, NULL, L"ReBarWindow32", NULL);
		if (hWnd != NULL)
		{
			hWnd = FindWindowEx(hWnd, NULL, L"Address Band Root", NULL);
			if (hWnd != NULL)
			{
				hWndURL = FindWindowEx(hWnd, NULL, L"Edit", NULL);
			}
		}
	}
	return hWndURL;
}

Open in new window

Best way is BHO. You can also give thread injection or dll injection a try.
@pgnatyuk
This is quite risky solution. Don't expect that it will always give you correct results...!!!
Yes, I know. It is not a real project. I agree with you.
But why not BHO ??? Are you getting any errors????
I've said - it is not a real project.
I thought that the correct way is to use IWebBrowser2. It didn't work because of GetActiveObject. I didn't expect that. I didn't want to spend time on BHO.
Thanks from the article.
Thanks
Thanks...!!! Please rate the article as well if you found it useful...!!!
Actually here is the correct (or, at least, the expected) answer:
Connecting to Running Instances of Internet Explorer:
http://www.codeproject.com/KB/shell/ietoolbartutorial.aspx
CodeProject: http://www.codeproject.com/KB/shell/iehelper.aspx
MSND:
1. ShellWindows: http://msdn.microsoft.com/en-us/library/bb773974(VS.85).aspx
2. Internet Explorer object: http://msdn.microsoft.com/en-us/library/aa752084(VS.85).aspx