CEFWrappers v1.04 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++ 2022 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/. 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 -G
"Visual Studio 17" -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 can also build for x86. 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 -G "Visual
Studio 17" -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 x86. 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 your application
is using the multi-threaded DLL (/MD or /MDd) setting then you will need to
rebuild CEF to match this.
- Please note that you will need to build the debug versions of your app
with _HAS_ITERATOR_DEBUGGING=0. This issue appeared in CEF M90. For details
please see
https://bitbucket.org/chromiumembedded/cef/issues/3109/windows-chrome-sandbox-crashes-with-m90.
- 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 2022 or later. They have not been tested
on earlier releases of Visual C++.
- 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 the 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 236 MB of files for the latest
Windows 64-bit cef_binary_120.1.10+g3ce3184+chromium-120.0.6099.129 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 VC2022 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.04 (26 December 2023)
- Updated copyright details.
- Updated code to pull in CEF v120.1.10.
- The sample project included in the download is now for VC 2022.
v1.03 (20 August 2022)
- Updated copyright details.
- Updated the code to use C++ uniform initialization for all variable
declarations.
- Added SAL annotations throughout the codebase.
- Updated the compiler and linker settings of the demo app to more modern
defaults.
- Updated the demo application to compile, link and release against the
latest version of CEF which is v104.4.24 as of August 2022.
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)