
CEFWrappers v1.02 A freeware MFC Class
Framework for the Chromium Embedded Framework
I was looking to embed Chromium recently in a desktop Windows MFC application and was
looking for examples. An excellent starting point is
https://www.codeproject.com/Articles/1105945/Embedding-a-Chromium-Browser-in-an-MFC-Application
from MVP Marius Bancilla. I
decided to take that as the basis and implement a fresh MFC sample from scratch
using the latest version of CEF. Thus was born CEFWrappers. This is a collection of
freeware MFC classes which implement a fully embeddable Chromium window in a MFC
desktop app using Visual C++ 2019 or later and MFC.
Usage
- Please read the article
https://www.codeproject.com/Articles/1105945/Embedding-a-Chromium-Browser-in-an-MFC-Application
on which my code is based to get a good understanding of how to integrate
CEF into your application.
- To use CEFWrappers in your code simply include CEFWrappers.h and
CEFWrappers.cpp in your project and #include CEFWrappers.h in which ever of your modules needs
to make calls to the various classes.
- You need to download and build CEF for Windows or download prebuilt
binaries for CEF. You can
download builds from
https://cef-builds.spotifycdn.com/index.html. If you want to build
CEF yourself, there is a article explaining how to do this at
http://mariusbancila.ro/blog/2016/05/05/building-the-64-bit-version-of-chromium-embedded-framework-on-windows/.
As of v1.01 of CEFWrappers, the demo application assumes you have downloaded
the x64 build of CEF to a "D:\DEV\CEFx64" directory. To build CEF for x64
after you have downloaded it, you should start a Visual Studio Command
Prompt, navigate to the CEF x64 directory and run "cmake -A x64 ." from the
command prompt. You should then load the cef.sln file in the CEF x64
directory and Build -> Batch Build -> Select All -> Build to build all the
CEF files for x64. The demo application also assumes you have downloaded the
x86 build of CEF to a "D:\DEV\CEFx86" directory. To build CEF for x86
after you have downloaded it, you should start a Visual Studio Command
prompt, navigate to the CEF x86 directory and run "cmake -A Win32 ." from
the command prompt. You should then load the cef.sln file in the CEF x86
directory and Build -> Batch Build -> Select All -> Build to build all the
CEF files for x64. At this point you should have all the CEF files for both
x64 and x86 in the two CEF directories. If you download CEF x64 or CEF x86
to different directories, then you need to ammend the CEFWrappers project's
VC Include and Lib Directories project settings.
- Your application must be linked to the same version of the C Runtime as
that used by the CEF libcef_dll_wrapper project in the CEF download. The
default is multi-threaded static version (/MT or /MTd). If you application
is using the multi-threaded DLL (/MD or /MDd) setting then you will need to
rebuild CEF to match this.
- To successfully use CEF in your application your application must
include a manifest file to indicate support for various versions of windows.
If you do not do this then you will now see any output in your CEF windows.
Please see CEFDemo.exe.manifest in the download for details.
- The classes are designed for VC 2019 or later. They have not been tested on earlier releases of VC.
- The class CCefAppHandler in the demo application is responsible for
process specific callbacks from CEF.
- The class CCEFDelegate in CEFWrappers is the main callback interface
which is used by CEFWrappers framework.
- The class CCEFClientHandler is responsible for Browser specific
callbacks from CEF and will end up calling CCEFDelegate virtual functions as
required. Note currently this class implements support for CefClient,
CefDisplayHandler, CefLifeSpanHandler, CefLoadHandler, CefRequestHandler &
CefResourceRequestHandler CEF interfaces. There are numerous other CEF
interfaces which could be included in this class going forward.
- The class CCEFView implements a standard CView derived class which you
can reuse in your application to host a CEF browser window.
- The class CCEFDoc is a CDocument derived class to go along with the
CCEFView class.
- All places which show CEF integration in a typical MFC MDI application
are highlighted in the CEFWrappers code using a "//CEF_INTEGRATION"
comment, which you are search for.
- The sample application shows a custom URL of
http://tests/intro.html which is
implemented using embedded resources in the demo application. This is implemented
using classes called CCEFDemoBinaryResourceProvider & CCEFDemoClientHandler
in the application. The intro.html is the main start page for each new MDI browser
child window and
provides links to four other example pages. These include a PDF file
embedded in a html page, a standalone PDF file and two png image files.
- Please note that the sample application is a bare bones example of CEF
integration and features such as Print, Print Preview, Cut, Copy, Paste,
download support etc are missing. For examples of this please check our the
cefclient sample application in the CEF download.
- Please note that during testing of the demo application with CEF, I got
inconsistent errors reported in the debugger when calling CefShutdown during
application shutdown. This I believe is due to some resources not being
correctly released using the reference counting mechanism which CEF uses. I
was not seeing these errors when testing with the release versions of CEF
and the errors were inconsistent, random and not URL specific. CEF
recommends a two stage approach to clean application shutdown as documented
at
https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage.md#markdown-header-browser-life-span.
I initially implemented this approach in the demo application in
CEFWrappers, but was still getting inconsistent errors on shutdown so I have
left the code using a DestroyWindow API approach in the CCEFView::CloseBrowser
method which is called by CMainFrame::OnClose as part of demo application's
normal shutdown. This is the approach used by the SDI application in the CodeProject article. If any person
has advice on how to improve this, please let me know.
- Once you have got everything compiled and working, you will need to ship
additional files with your application to support CEF at runtime. These files are documented
in the README.txt file in the root directory of the CEF download. This will
comprise approximately 166 MB of files for the latest Windows 64-bit
79.1.36+g90301bd+chromium-79.0.3945.130 version of CEF. By default these
files should be located in the same directory as your application exe, but
the CEFWrappers demo application as of v1.01 shows you how to locate CEF to
a different directory using delay loading.
- For further information on developing with CEF and the functionality
it provides, please review the comprehensive documentation on the CEF
wiki web site at
https://bitbucket.org/chromiumembedded/cef/wiki/Home.
The enclosed zip file contains the wrapper
class source code and a simple VC2019 demo application which exercises all of the
framework classes functionality.
Copyright
- You are allowed to include the source code in any product (commercial, shareware,
freeware or otherwise) when your product is released in binary form.
- You are allowed to modify the source code in any way you want except you
cannot modify the copyright details at the top of each module.
- If you want to distribute source code with your application, then you are
only allowed to distribute versions released by the author. This is to maintain
a single distribution point for the source code.
Sample Screen Capture
This is the demo application included in the CEFWrapper download in operation
on Windows 10:

Updates
v1.02 (3 December 2020)
- Updated the demo application to compile, link and release against the
latest version of CEF which is v87.1.11 as of December 2020.
v1.01 (7 March 2020)
- Updated the demo application to only create the CCefAppHandler instance
prior to calling CefInitialize in the CCEFDemoApp::InitializeCef method in
the demo app. This is a small optimzation to make the demo application code
more consistent with the cefsimple application.
- Updated the CCefAppHandler class to support the CefBrowserProcessHandler
interface.
- Updated the CCEFView::SetupClientHandler method to not set the
web_security settings value to STATE_DISABLED. This allows the demo program
to better render some web pages.
- Updated the sample application to include x86 build configurations in
addition to the existing x64 build configurations.
- Updated the demo application to load CEF from a subdirectory called
"CEF" below where the main exe is located. To see how this is done, please
see the code in CCEFDemoApp::InitInstance.
- Made CCEFView::SetupClientHandler method virtual.
v1.0 (17 February 2020)