From 8da4534bf582520348f6c4f8885e201d5dfb9b03 Mon Sep 17 00:00:00 2001 From: Mike Yeager Date: Sat, 6 Mar 2021 23:50:12 -0700 Subject: [PATCH] Added check for WebView2 runtime evergreen and install if not found. --- Photino.Native/Photino.Windows.cpp | 53 +++++++++++++++++++++++++++++- Photino.Native/Photino.h | 2 ++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Photino.Native/Photino.Windows.cpp b/Photino.Native/Photino.Windows.cpp index 71268c0..7b75e5d 100644 --- a/Photino.Native/Photino.Windows.cpp +++ b/Photino.Native/Photino.Windows.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#pragma comment(lib, "Urlmon.lib") #define WM_USER_SHOWMESSAGE (WM_USER + 0x0001) #define WM_USER_INVOKE (WM_USER + 0x0002) @@ -192,7 +195,10 @@ void Photino::Show() // until the window is shown. if (!_webviewController) { - AttachWebView(); + if (EnsureWebViewIsInstalled()) + AttachWebView(); + else + exit(0); } } @@ -240,6 +246,51 @@ void Photino::Invoke(ACTION callback) waitInfo.completionNotifier.wait(uLock, [&] { return waitInfo.isCompleted; }); } +bool Photino::EnsureWebViewIsInstalled() +{ + LPWSTR* versionInfo = new wchar_t*[100] ; + HRESULT ensureInstalledResult = GetAvailableCoreWebView2BrowserVersionString(nullptr, versionInfo); + + if (ensureInstalledResult != S_OK) + return InstallWebView2(); + + return true; +} + +bool Photino::InstallWebView2() +{ + const wchar_t* srcURL = L"https://go.microsoft.com/fwlink/p/?LinkId=2124703"; + const wchar_t* destFile = L"MicrosoftEdgeWebview2Setup.exe"; + + if (S_OK == URLDownloadToFile(NULL, srcURL, destFile, 0, NULL)) + { + LPWSTR command = new wchar_t[100]{ L"MicrosoftEdgeWebview2Setup.exe\0" }; //add these switches? /silent /install + + STARTUPINFO si; + PROCESS_INFORMATION pi; + + ZeroMemory(&si, sizeof(si)); + si.cb = sizeof(si); + ZeroMemory(&pi, sizeof(pi)); + + bool installed = CreateProcess( + NULL, // No module name (use command line) + command, // Command line + NULL, // Process handle not inheritable + NULL, // Thread handle not inheritable + FALSE, // Set handle inheritance to FALSE + 0, // No creation flags + NULL, // Use parent's environment block + NULL, // Use parent's starting directory + &si, // Pointer to STARTUPINFO structure + &pi); // Pointer to PROCESS_INFORMATION structure + + return installed; + } + + return false; +} + void Photino::AttachWebView() { std::atomic_flag flag = ATOMIC_FLAG_INIT; diff --git a/Photino.Native/Photino.h b/Photino.Native/Photino.h index 292027f..5159789 100644 --- a/Photino.Native/Photino.h +++ b/Photino.Native/Photino.h @@ -50,6 +50,8 @@ class Photino wil::com_ptr _webviewController; std::map _schemeToRequestHandler; void AttachWebView(); + bool EnsureWebViewIsInstalled(); + bool InstallWebView2(); #elif OS_LINUX GtkWidget* _window; GtkWidget* _webview;