CEF in a MFC dialog not working

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.

CEF in a MFC dialog not working

Postby yinteresting » Thu Sep 07, 2017 2:15 pm

Problem:
Embedded CEF browser in a MFC dialog is not showing the default URL specified during CreateBrowser, it is just a blank page.
CefBrowserHost::CreateBrowser(window_info, handler, url, browser_settings, NULL);
url = "http://www.google.com";

My embeded cef browser in a MFC dialog works in cef version 3.2704 but not working on any version after that, for example 3.3112.1656/3.3112.1659 with Visual Studio 2017. I noticed one difference of these two versions are cef_ptr.h, the smart pointer implementation has changed.

The idea of the embedded cef browser is to display a web browser in an MFC dialog.

Below is some sample code:
(I generated an MFC dialog project in Visual studio 2017.)

1. CEF initialization starts in the OnInitDialog:

BOOL CtestSimpleDialogDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// I specify NULL rather than m_hInstance because I don't know how to pass the h_hInstance from app to the dialogDlg.
// Also, I don't think the main_args matters since I don't pass any args to the program.
CefMainArgs main_args(NULL);
CefSettings settings;
settings.multi_threaded_message_loop = false;

int exit_code = CefExecuteProcess(main_args, NULL, NULL);
if (exit_code >= 0) {
// The sub-process has completed so return here.
return exit_code;
}
// CtestSimpleDialogDlg implements
// class CtestSimpleDialogDlg : public CDialogEx,
// public CefApp,
// public CefBrowserProcessHandler
CefRefPtr<CtestSimpleDialogDlg>m_app = new CtestSimpleDialogDlg;
CefInitialize(main_args, settings, m_app.get(), NULL);

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

CEF_REQUIRE_UI_THREAD();

CefRefPtr<CefCommandLine> command_line =
CefCommandLine::GetGlobalCommandLine();

#if defined(OS_WIN) || defined(OS_LINUX)
// Create the browser using the Views framework if "--use-views" is specified
// via the command-line. Otherwise, create the browser using the native
// platform framework. The Views framework is currently only supported on
// Windows and Linux.
const bool use_views = command_line->HasSwitch("use-views");
#else
const bool use_views = false;
#endif

// SimpleHandler implements browser-level callbacks.
handler = (new SimpleHandler(use_views));

// Specify CEF browser settings here.
CefBrowserSettings browser_settings;

std::string url;
// Check if a "--url=" value was provided via the command-line. If so, use
// that instead of the default URL.
url = command_line->GetSwitchValue("url");
if (url.empty())
url = "http://www.google.com";
if (use_views) {
// Create the BrowserView.
CefRefPtr<CefBrowserView> browser_view = CefBrowserView::CreateBrowserView(
handler, url, browser_settings, NULL, NULL);

// Create the Window. It will show itself after creation.
CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(browser_view));
}
else {
// Information used when creating the native window.
CefWindowInfo window_info;
HWND hWnd;
CRect rect;
// IDC_EDIT_CEF is an edit control in the dialog.
hWnd = GetDlgItem(IDC_EDIT_CEF)->GetSafeHwnd();
GetDlgItem(IDC_EDIT_CEF)->GetClientRect(&rect);
window_info.SetAsChild(hWnd, rect);
// Create the first browser window.
CefBrowserHost::CreateBrowser(window_info, handler, url, browser_settings, NULL);
}
return TRUE; // return TRUE unless you set the focus to a control
}

2. message loop is running in the app:

int CtestSimpleDialogApp::PumpMessage(){
auto result = CWinApp::PumpMessage();

CefDoMessageLoopWork();

return result;
}

3. CEF shutdown is also in the app:

int CtestSimpleDialogApp::ExitInstance()
{
//TODO: handle additional resources you may have added
AfxOleTerm(FALSE);
CefShutdown();
return CWinApp::ExitInstance();
}


I use a button click function to debug the browser:
void CtestSimpleDialogDlg::OnTestClicked() {
CefRefPtr<CefBrowser> m_browser;
if (!handler.get()->browser_list_.empty()){
m_browser = *handler.get()->browser_list_.begin();

bool m = m_browser->HasDocument(); // false
CefRefPtr<CefFrame> frame = m_browser->GetMainFrame();
//frame->ViewSource;
CefString myUrl = m_browser->GetMainFrame()->GetURL(); // null
m_browser->GetMainFrame()->LoadURL("http://www.google.com");
CefString myUrl2 = m_browser->GetMainFrame()->GetURL(); //null
}
}
And what I found out is that the browser is valid, but has no document. I try to use LoadURL to verify but it doesn't work, after clicking the dialog is still empty.

I am pulling my hair so bad that I am getting bald (just kidding). Please help. Thanks.
yinteresting
Newbie
 
Posts: 5
Joined: Thu Sep 07, 2017 1:53 pm

Re: CEF in a MFC dialog not working

Postby rupali1201 » Thu Jan 02, 2020 5:54 am

yinteresting, how you solved this. I am stuck in the same issue.
rupali1201
Newbie
 
Posts: 5
Joined: Fri Dec 27, 2019 1:57 am


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 47 guests