美文网首页程序员
libcef在win10下的manifest问题

libcef在win10下的manifest问题

作者: 拉普拉斯妖kk | 来源:发表于2023-11-30 14:36 被阅读0次

    在win10下集成libcef的项目运行时可能看到一个白屏的浏览器窗口,查日志发现报错:

    Check failed: fallback_available == base::win::GetVersion() > base::win::Version::WIN8 (1 vs. 0)
    

    这是因为浏览器程序加载不到manifest文件,导致操作系统版本处理错误。

    关于这个错误CEF官方的解答是这样的:Check failed: fallback_available

    微软官方的解释是这样的:让你的应用程序面向 Windows

    解决方案

    第一种解决方案,可以通过创建一个manifest文件添加到工程项目中。具体步骤如下:

    • 在工程目录中创建一个manifest文件——xxx.manifest(xxx是可以工程名字或者其他有意义的),文件内容如下:
    <?xml version="1.0" encoding="utf-8"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">  
      <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">  
        <application> 
            <!-- Windows 10 and Windows 11 -->
            <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
            <!-- Windows 8.1 -->
            <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
            <!-- Windows 8 -->
            <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
        </application> 
      </compatibility> 
    </assembly>
    
    • 在项目工程中添加这个manifest文件,添加路径是:在vs项目属性中找到清单工具 —— 输入和输出 —— 附加清单文件,添加上刚刚创建的xxx.manifest。

    第二种解决方案主要参考了CEF提供的示例程序——cefsimple。具体步骤如下:

    • 拷贝CEF源码中示例程序cefsimple中的 cefsimple.exe.manifest 和 compatibility.manifest (在test\cefsimple中)两个文件到的工程目录下面,并且将cefsimple.exe.manifest名称修改为xxx.exe.manifest(xxx是可以工程名字或者其他有意义的)。
    • 在后期生成事件中加上添加manifest的脚本,添加路径是:在vs项目属性中找到 生成事件 —— 后期生成事件 —— 命令行,添加上如下命令,注意替换compatibility.manifest、xxx.exe.manifest和your/path/xxx.exe为你自己的路径,可以用绝对路径:
    setlocal
    mt.exe -nologo -manifest "compatibility.manifest" "xxx.exe.manifest" -outputresource:"your/path/xxx.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
    

    以上,重新编译程序后就可以看到浏览器正确加载网页,再无报错信息。

    优化

    针对第二种解决方案,如果vs工程文件是通过cmake生成的还可以参考cefsimple中的cmake脚本优化下。

    • 在CEF工程的cmake文件夹中cef_macros.cmake文件中定义了一个宏:
    # Add custom manifest files to an executable target.
    macro(ADD_WINDOWS_MANIFEST manifest_path target extension)
      add_custom_command(
        TARGET ${target}
        POST_BUILD
        COMMAND "mt.exe" -nologo
                -manifest \"${manifest_path}/${target}.${extension}.manifest\" \"${manifest_path}/compatibility.manifest\"
                -outputresource:"${CEF_TARGET_OUT_DIR}/${target}.${extension}"\;\#1
        COMMENT "Adding manifest..."
        )
    endmacro()
    
    • 将它定义到生成vs工程文件的CMakeLists.txt中,然后添加如下内容(参考test\cefsimple中的CMakeLists.txt),注意修改${YOUR_TARGET}为工程名称。
    # Add the custom manifest files to the executable.
      ADD_WINDOWS_MANIFEST("${CMAKE_CURRENT_SOURCE_DIR}" "${YOUR_TARGET}" "exe")
    
    • 如果工程编译出来的不是exe,而是dll,需要修改上面ADD_WINDOWS_MANIFEST中最后一个参数为"dll",相应的也需要修改xxx.exe.manifest文件名为xxx.dll.manifest。

    相关文章

      网友评论

        本文标题:libcef在win10下的manifest问题

        本文链接:https://www.haomeiwen.com/subject/rfsqgdtx.html