Embedding Chromium in MFC application

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Embedding Chromium in MFC application

Postby StraySod » Mon Oct 12, 2009 9:31 am

Hi,

I tried to embed Chromium using the Cef library into basic MFC SDI application. It doesn't work and I'm not sure what I'm doing wrong. I'm new to embedding chromium. What I have done until now:

1. I call CefInitialize(...) in Main Frame constructor:

Code: Select all
CMainFrame::CMainFrame()
{
   // TODO: add member initialization code here
   CefInitialize(false, std::wstring());
}


2. I try to create a browser in overriden OnInitialUpdate method of my CView derived class:

Code: Select all
void Ccef_testView::OnInitialUpdate()
{
   g_handler = new ClientHandler();//this is a global object declared like this: CefRefPtr<CefHandler> g_handler; ClientHandler is my implementation of CefHandler returning RV_CONTINUE in every method
   CefWindowInfo info;
   HWND hWnd = this->GetSafeHwnd();
   RECT rect;

   this->GetClientRect(&rect);
   info.SetAsChild(hWnd, rect);

   CefBrowser::CreateBrowser(info, false, static_cast<CefRefPtr<CefHandler>>(g_handler), _T("http://www.google.com"));//I assume now should I see the browser, but nothing happens
}


3. In Main Frames destructor I call CefShutdown():

Code: Select all
CMainFrame::~CMainFrame()
{
   CefShutdown();
}


according to what the debugger says it should work, browser coordinates and dimensions are well set. Probably I'm missing something. Please let me know if you had any suggestions. I'm using winXP pro and building in Visual Studio 2008. I set all linker dependencies like cefclient example has, copied needed dlls into applications root directory and included cef.h. Thank you for your suggestions.

Best regards,
Martin
StraySod
Newbie
 
Posts: 2
Joined: Mon Oct 12, 2009 8:50 am

Re: Embedding Chromium in MFC application

Postby magreenblatt » Tue Oct 13, 2009 9:17 am

Hi Martin,

Your problem is that the CEF message loop is not being run. The best way to accomplish this with MFC is to initialize CEF with a multi-threaded message loop by setting the first parameter of CefInitialize() to true.

Code: Select all
CefInitialize(true, std::wstring());

The other option is to initialize CEF with a single-threaded message loop as you are doing currently and then call CefDoMessageLoopWork() regularly to perform the CEF message loop processing. This approach can be complex with an MFC-based application because there's no direct way (that I've been able to identify) to make additional function calls as part of the MFC message loop.

Regards,
Marshall
magreenblatt
Site Admin
 
Posts: 11088
Joined: Fri May 29, 2009 6:57 pm

Re: Embedding Chromium in MFC application

Postby StraySod » Tue Oct 13, 2009 11:52 am

Thank you very much Marshall. It's working as expected now.

Regards,
Martin
StraySod
Newbie
 
Posts: 2
Joined: Mon Oct 12, 2009 8:50 am

Re: Embedding Chromium in MFC application

Postby renet » Thu Oct 15, 2009 8:19 am

Thanks for posting this question and thanks for the answer as I was about to try embedding chrome in an MFC app myself. Nice work on this library! Its great to have an alternative to embedding IE or Mozilla.
renet
Newbie
 
Posts: 3
Joined: Thu Oct 15, 2009 8:17 am

Re: Embedding Chromium in MFC application

Postby renet » Thu Oct 15, 2009 2:34 pm

One other question. I was going to try to embed Chrome in an MFC application built using Visual Studio 2008 using MFC as a shared dll. Was this your general configuration or did you use VS2005? Perusing the forums and the issues list it seems as though some folks have had trouble using CEF with Visual Studio 2008, has anyone tried integrating CEF with an MFC application using MFC as a shared dll. I realize I might have to build Chromium and CEF using the instructions provided on their respective pages but other than that has anyone run into any major issues?

Thank you in advance,

Rene
renet
Newbie
 
Posts: 3
Joined: Thu Oct 15, 2009 8:17 am

Re: Embedding Chromium in MFC application

Postby magreenblatt » Thu Oct 15, 2009 2:58 pm

Hi Rene,

The VS2008 troubles should be a thing of the past now that the CEF configuration is GYP-based. Follow the instructions to generate the VS2008 project files and then edit the libcef_dll_wrapper project to have the same properties as your application ("Use of MFC", etc). Other than that you should be fine.

Regards,
Marshall
magreenblatt
Site Admin
 
Posts: 11088
Joined: Fri May 29, 2009 6:57 pm

Re: Embedding Chromium in MFC application

Postby renet » Fri Oct 16, 2009 9:41 am

Marshall,

Thank you for the suggestion. It worked like a charm! If you would like we'd be happy to write up something about how we used CEF in a sort of CEF & MFC 101 and send it to you for use on the wiki if the write-up is appropriate and accurate. And whatever wrapper classes we end up creating (if any at all - we'll probably write up an MFC and a .Net wrapper class), we'll of course forward the code onto this project for use and improvement by others.

Thank you again,

Rene
renet
Newbie
 
Posts: 3
Joined: Thu Oct 15, 2009 8:17 am

Re: Embedding Chromium in MFC application

Postby magreenblatt » Fri Oct 16, 2009 9:49 am

If you would like we'd be happy to write up something about how we used CEF in a sort of CEF & MFC 101 and send it to you for use on the wiki if the write-up is appropriate and accurate. And whatever wrapper classes we end up creating (if any at all - we'll probably write up an MFC and a .Net wrapper class), we'll of course forward the code onto this project for use and improvement by others.


That would be great, thanks :-).
magreenblatt
Site Admin
 
Posts: 11088
Joined: Fri May 29, 2009 6:57 pm

Re: Embedding Chromium in MFC application

Postby jerryevans » Sat Oct 31, 2009 11:21 am

Hello Rene

Any chance of sharing the VS2008 binaries? I was going to do the same last month but got sidetracked.

Many thanks

Jerry
jerryevans
Techie
 
Posts: 30
Joined: Tue Sep 08, 2009 5:30 am

Re: Embedding Chromium in MFC application

Postby rlscef » Wed May 26, 2010 6:59 pm

Did this (writing up a mini "CEF & MFC 101") ever happen? I'm also trying to get CEF working within an MFC application, and am having some difficulty.

I initially embedded a browser window using the Apple Webkit port. It worked almost perfectly, but the licensing restrictions preclude its use for almost any commercial purpose, so now I'm trying to get CEF going.

The browser window comes up nicely, but if I cover the application window with another window, and then flip back, the browser briefly is painted and then blanks out.

...and there are other problems, e.g., trying to remove browser windows doesn't work for me.
rlscef
Newbie
 
Posts: 6
Joined: Wed Apr 07, 2010 6:40 pm


Return to Support Forum

Who is online

Users browsing this forum: DMike and 37 guests