Hosting Mozilla/IE in one page of C++
5/8/2005
I don't think anyone has put is as succiently as this before. It's written with ATL and you'll need to link with comsupp.lib but other than that it's pretty self contained.

All you need to do is start using the IWebBrowser2 pointer 'Browser'.
#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
#include <atlwin.h>
#include <atlhost.h>
#include <exdisp.h>
#include <comdef.h>

CComModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()

class ActiveXCtrl
{
	CAxHostWindow *Host;
	CAxWindow Wnd;
	IUnknown *Ctrl;
	IWebBrowser2 *Browser;

public:
	ActiveXCtrl()
	{
		Host = 0;
		Ctrl = 0;
		Browser = 0;

		_Module.Init(ObjectMap, (HINSTANCE)GetCurrentProcess());
		AtlAxWinInit();
	}

	~ActiveXCtrl()
	{
		Detach();
	}

	bool Attach(HWND hwnd)
	{
		bool Status = false;
		RECT Rc = {0, 0, 100, 100};

		WORD *Url = L"http://www.mozilla.org";
		#if 1
		char *Ctrl = "Mozilla.Browser.1"; // Mozilla
		#else
		char *Ctrl = "Shell.Explorer.2"; // IE
		#endif

		if (Wnd.Create(	hwnd,
						Rc,
						_T(Ctrl),
						WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
						0))
		{			
			if (Wnd.QueryControl(IID_IWebBrowser2, (void**)&Browser) == S_OK)
			{
				CComVariant v;
				
				_bstr_t u = Url;
				if (Browser->Navigate(u, &v, &v, &v, &v) == S_OK)
				{
					Status = true;
				}
			}
		}
		
		return Status;
	}

	bool Detach()
	{
		if (Browser)
		{
			Browser->Release();
			Browser = 0;
		}
		if ((HWND)Wnd)
		{
			Wnd.DestroyWindow();
		}
		return true;
	}

	bool SetPos(RECT Rc, bool Repaint = false)
	{
		if ((HWND)Wnd)
		{
			Wnd.MoveWindow(&Rc, Repaint);
		}

		return true;
	}
};


Comments:

SnappyCrunch
06/08/2005 5:56am
Whew... I don't think you've every posted something I understand less. If you feel like explaining what it does, as it seems you're proud of it, I'd like some clarification.

I'm not familiar with the word "Hosting", in this context, but studying the next post makes it seem like you're embedding the browser rendering engines in another application. Is that right? Also, it took me a while to figure out what ATL is, as google doesn't turn up anything useful on it's own. So I realize that ATL is an activeX library, but what is that library useful for? Thanks for the clarification, if you feel like it.
fret
06/08/2005 8:11am
RTE = a rich text editor built inside a browser.

Mozilla ActiveX = A compatible implementation with IE's API except using the mozilla gecko engine.

ActiveX Hosting = means that your application is a container for an ActiveX control. In my case I'm hosting the mozilla activex control which then runs the RTE webpage.

ATL = Active template library is a wrapper around the COM layer in windows, it makes writing COM code much easier. MFC is the other common COM library, but it has a lot of other things as well which most apps never use. ATL is a lightweight thing that adds very little bulk to your app. And it doesn't have any dependencies if you don't want them. Unlike MFC which has to pull in the entire MFC codepage (1mb or more).
fret
06/08/2005 8:13am
I might add also that ATL makes hosting ActiveX controls really easy, hence the above code snippit being so small. To write an ActiveX control container in raw C++ is very difficult and takes a lot of code.
SnappyCrunch
07/08/2005 1:27am
Thank you :)
6r15u
05/10/2005 7:21am
Hi
this works?
don't you need to write something like
#import "PROGID:Mozilla.Browser.1" ...
fret
05/10/2005 7:45am
Nope, this works without anything like that. The interfaces are defined in the Visual C++ headers.
donMiguel
02/12/2005 2:28pm
Sorry, but this is NO hosting of Mozilla!!! What you are hosting is the IE WebBrowser control... and with it, you navigate to Mozilla homepage!!

How naive could you be!! Seriously, youre entry start big laugh in our office!
:-D :-D
fret
02/12/2005 8:39pm
donMiguel: You would need to have the mozilla activex control installed, otherwise windows will make an instance of IE. In fact windows might just revert to IE if there is any error. This code when run on my machine DOES use the mozilla control. You can see the "Ctrl" variable which is compile time switchable via a #if between the Mozilla control string and the IE control string.

Btw, installing Mozilla doesn't necessarily give you the activex control. I had to use a specific mozilla activex download to get it to work.
me
23/02/2006 1:04pm
sorry I am a newbee and I want to embed the Mozilla ActiveX Control without MFC and your code seems to be able to do that. But I don't know how to use it!

I have VC++ 6.0 any advice on how to use the code will be very much appreciated.

Thank You.
fret
23/02/2006 9:30pm
me: It's pretty simple, paste the code into a new source file (.cpp), create a new win32 application project, add the source file, add the library 'comsupp.lib' to the link section and build! Thats it. It's so simple I didn't bother making a download for it.
me
24/02/2006 6:26am
Thank so very much for that just one more question..

how do i access other browser functions like refresh, stop, back etc?

I used
Browser->Refresh();

but that makes the app crash.

Thanks You.
fret
24/02/2006 6:30am
me: Um, I don't know. You'll have to look at the API documentation for IWebBrowser2. This is just to get you over the hosting hurdle... not as a finished application worth anything. I really havn't done much more than whats above.


From:
Email (optional): (Will be HTML encoded to evade harvesting)
Message:
 
Remember username and/or email in a cookie.
Notify me of new posts in this thread via email.
BBcode:
[q]text[/q]
[url=link]description[/url]
[img]url_to_image[/img]
[pre]some_code[/pre]
[b]bold_text[/b]
Post