@@ -48,6 +48,29 @@ int CALLBACK WinMain(
4848 SetEnvironmentVariable (L" COREWEBVIEW2_FORCED_HOSTING_MODE" , L" COREWEBVIEW2_HOSTING_MODE_WINDOW_TO_VISUAL" );
4949 }
5050
51+ // If config.json specifies browserExecutableFolder, call GetAvailableCoreWebView2BrowserVersionString()
52+ // while starting up to verify it is valid. If it is not, this allows showing an error message before
53+ // the main application window is created.
54+ // See: https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution
55+ std::wstring browserExecutableFolderWstr; // storage for browserExecutableFolder
56+ LPCWSTR browserExecutableFolder = nullptr ;
57+
58+ if (!configJson.browserExecutableFolder .empty ())
59+ {
60+ browserExecutableFolderWstr = Utf8ToWide (configJson.browserExecutableFolder );
61+ browserExecutableFolder = browserExecutableFolderWstr.c_str ();
62+
63+ LPWSTR versionInfo;
64+ GetAvailableCoreWebView2BrowserVersionString (browserExecutableFolder, &versionInfo);
65+ if (versionInfo == nullptr )
66+ {
67+ MessageBox (NULL , L" The \" browserExecutableFolder\" option is set, but WebView2 failed to initialize it. Check there is a valid fixed distribution of WebView2 in the specified folder." , szTitle, MB_ICONERROR);
68+ return 1 ;
69+ }
70+
71+ CoTaskMemFree (versionInfo);
72+ }
73+
5174 // ///////////////////////////////////////////////////
5275 // Register window class
5376 WNDCLASSEX wcex;
@@ -115,7 +138,7 @@ int CALLBACK WinMain(
115138 options->put_AdditionalBrowserArguments (Utf8ToWide (configJson.additionalBrowserArguments ).c_str ());
116139 }
117140
118- CreateCoreWebView2EnvironmentWithOptions (nullptr , nullptr , options.Get (),
141+ CreateCoreWebView2EnvironmentWithOptions (browserExecutableFolder , nullptr , options.Get (),
119142 Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
120143 [hWnd, configJson](HRESULT result, ICoreWebView2Environment* env) -> HRESULT
121144 {
0 commit comments