【发布时间】:2020-01-13 06:59:35
【问题描述】:
我已按照 Getting Started with WebView2 (developer preview) 的所有说明创建使用 Microsoft Edge (Chromium) 的应用程序。但是,它无法找到 Edge 浏览器。我还尝试了示例应用程序(this 和 this),但结果相同。我已经为 32 位和 64 位构建了应用程序。
调用CreateWebView2EnvironmentWithDetails()得到的是错误0x80070002,即ERROR_FILE_NOT_FOUND(系统找不到指定的文件。)
HRESULT hr = CreateWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr,
Callback<IWebView2CreateWebView2EnvironmentCompletedHandler>(
[hWnd](HRESULT result, IWebView2Environment* env) -> HRESULT {
// Create a WebView, whose parent is the main window hWnd
env->CreateWebView(hWnd, Callback<IWebView2CreateWebViewCompletedHandler>(
[hWnd](HRESULT result, IWebView2WebView* webview) -> HRESULT {
if (webview != nullptr) {
webviewWindow = webview;
}
// Resize WebView to fit the bounds of the parent window
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewWindow->put_Bounds(bounds);
// Schedule an async task to navigate to Bing
webviewWindow->Navigate(L"https://www.bing.com/");
return S_OK;
}).Get());
return S_OK;
}).Get());
if (!SUCCEEDED(hr))
{
if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
MessageBox(
nullptr,
L"Couldn't find Edge installation. "
"Do you have a version installed that's compatible with this "
"WebView2 SDK version?",
nullptr, MB_OK);
}
else
{
std::wstringstream formattedMessage;
formattedMessage << L"Failed to create webview environment"
<< ": 0x" << std::hex << std::setw(8) << hr;
MessageBox(nullptr, formattedMessage.str().c_str(), nullptr, MB_OK);
}
}
我有:
- Edge 版本 79.0.309.60(官方版本)测试版(64 位)
- Windows 10.0.17134
- Visual Studio 2019 16.4.2
知道为什么找不到我的 Edge 安装吗?
【问题讨论】:
-
WebView2窗口是否显示?您使用的是哪个版本的 WebView2 SDK?我尝试过 this sample 并使用 Microsoft.Windows.ImplementationLibrary v1.0.191107.2 版本和 Microsoft.Web.WebView2 v0.8.355,代码在我这边运行良好,您可以查看它。此外,您可以尝试通过CreateWebView2EnvironmentWithDetails 设置 browserExecutableFolder
-
是的,我也检查过,结果相同。
-
我尝试为第一个参数指定
C:\Program Files (x86)\Microsoft\Edge Beta\Application,但出现了同样的错误。然后我使用进程监视器并注意到它正在尝试查找文件 EBWebView\x64\EmbeddedBrowserWebView.dll,因此它实际上位于c:\Program Files (x86)\Microsoft\Edge Beta\Application\79.0.309.63下。当我指定时,它就起作用了。但是,我认为这还不够好。我没有指定安装的文件夹。如果我对安装在客户处的应用程序执行此操作,我希望它自动确定其安装路径。 -
参考this article,可以看到在使用CreateWebView2EnvironmentWithDetails方法时,如果browserExecutableFolder为null或空字符串创建WebView,API会尝试寻找机器上安装的Edge兼容版本根据频道偏好,首先尝试查找每个用户安装,然后查找每个机器安装。它会自动检测 Edge(铬)浏览器,在我这边,使用默认设置安装 Microsoft Edge,它会自动检测浏览器。
标签: c++ windows browser webview microsoft-edge