Skip to content

Commit 94d3d23

Browse files
committed
Add browserExecutableFolder config option
1 parent b8d8567 commit 94d3d23

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/Config.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ConfigJson LoadConfigJson(const std::string& path)
1313
ret.preferUnstableChannel = false;
1414
ret.additionalBrowserArguments = "";
1515
ret.hostingMode = "";
16+
ret.browserExecutableFolder = "";
1617

1718
// If file does not exist, just use defaults
1819
if (!FileExists(path))
@@ -43,5 +44,8 @@ ConfigJson LoadConfigJson(const std::string& path)
4344
if (parsedJson.contains("hostingMode"))
4445
ret.hostingMode = parsedJson["hostingMode"].get<std::string>();
4546

47+
if (parsedJson.contains("browserExecutableFolder"))
48+
ret.browserExecutableFolder = parsedJson["browserExecutableFolder"].get<std::string>();
49+
4650
return ret;
4751
}

src/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct ConfigJson {
77
bool preferUnstableChannel;
88
std::string additionalBrowserArguments;
99
std::string hostingMode;
10+
std::string browserExecutableFolder;
1011
};
1112

1213
ConfigJson LoadConfigJson(const std::string& path);

src/WebView.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)