CefInitialize崩溃 && Cef白屏

新建一个MFC Dialog工程,然后仿照CefSimple模仿实现自己的一个基于MFC窗口的Demo(具体代码稍后上),注意我仅仅只配置了所需要依赖的lib,并没有把CefSample的配置一起Copy,所以才导致后面的白屏,浪费了我一天时间。
坑一:CefInitialize崩溃
也许这个坑很多网友都遇到过,所以寻找度娘的时候问题很快就解决了,就是exe运行环境缺少必须要的资源包(不仅仅dll,所以图方便我们直接从CefSimple环境拷贝出来)。
在这里插入图片描述
坑二:Cef白屏
当出现白屏的时候我第一反应就是代码有bug,于是我从MFC工程切换到win32工程,从子窗口切换到Popup,各种尝试都不行,最后我把CefSimple整个工程拷贝过来,然后按照我的配置修改编译依旧白屏,此刻问题基本确定:工程配置不对。但是工程配置有什么不对呢?只能一点点的比较,终于在最后发现:后期生成事件没有配置。命令如下:

setlocal
mt.exe -nologo -manifest “H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/tests/cefsimple/cefsimple.exe.manifest” “H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/tests/cefsimple/compatibility.manifest” -outputresource:”E:/project/CefDemo/x64/Debug/cefsimple.exe”;#1
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/chrome_elf.dll E:/project/CefDemo/x64/Debug/chrome_elf.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/d3dcompiler_43.dll E:/project/CefDemo/x64/Debug/d3dcompiler_43.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/d3dcompiler_47.dll E:/project/CefDemo/x64/Debug/d3dcompiler_47.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/libcef.dll E:/project/CefDemo/x64/Debug/libcef.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/libEGL.dll E:/project/CefDemo/x64/Debug/libEGL.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/libGLESv2.dll E:/project/CefDemo/x64/Debug/libGLESv2.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/natives_blob.bin E:/project/CefDemo/x64/Debug/natives_blob.bin
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/snapshot_blob.bin E:/project/CefDemo/x64/Debug/snapshot_blob.bin
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/v8_context_snapshot.bin E:/project/CefDemo/x64/Debug/v8_context_snapshot.bin
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_directory H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Debug/swiftshader E:/project/CefDemo/x64/Debug/swiftshader
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Resources/cef.pak E:/project/CefDemo/x64/Debug/cef.pak
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Resources/cef_100_percent.pak E:/project/CefDemo/x64/Debug/cef_100_percent.pak
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Resources/cef_200_percent.pak E:/project/CefDemo/x64/Debug/cef_200_percent.pak
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Resources/cef_extensions.pak E:/project/CefDemo/x64/Debug/cef_extensions.pak
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Resources/devtools_resources.pak E:/project/CefDemo/x64/Debug/devtools_resources.pak
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_if_different H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Resources/icudtl.dat E:/project/CefDemo/x64/Debug/icudtl.dat
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
“D:Program FilesCMakeincmake.exe” -E copy_directory H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/Resources/locales E:/project/CefDemo/x64/Debug/locales
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd

上面命令主要实现两个功能:
1.将manifest 文件嵌入到exe内部。
2.将所依赖的文件copy到运行环境(所以如果配置了这个生成后事件,那么问题一就不需要手动拷贝依赖项了)。

manifest 文件学习参考:

什么是vs 程序的manifest文件
exe或者dll 嵌入manifest文件方法
VS2008 manifest文件简单应用

所以白屏的根本原因是缺少manifest文件,导致运行的时候出问题。
解决方法两种:

把上面的代码完整Copy到生成后事件里面去【记得修改自己的工程目录】
如果之前已经把必要的文件和资源Copy到了运行环境,那么我们只需要Copy如下一段代码就OK啦(功能就是把manifest文件嵌入exe内部)【记得修改自己的工程目录】:

setlocal
mt.exe -nologo -manifest “H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/tests/cefsimple/cefsimple.exe.manifest” “H:/cef/cef_binary_3.3538.1852.gcb937fc_windows64/tests/cefsimple/compatibility.manifest” -outputresource:”E:/project/CefDemo/x64/Debug/cefsimple.exe”;#1
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd

下一章节将描述如何把Cef作为一个子窗口集成到MFC工程和win32工程。
————————————————
版权声明:本文为CSDN博主「CAir2」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/CAir2/java/article/details/85006566

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注