From d3d11ff67d6829598f53f4938cdbd4b3a8f91a16 Mon Sep 17 00:00:00 2001 From: Valentin Radu Date: Sun, 20 Feb 2022 18:14:07 +0200 Subject: [PATCH] Weather: Download and install WebView2 Runtime when not available on the system --- ExplorerPatcher/dllmain.c | 33 ++++++++++++--- ExplorerPatcher/utility.c | 88 +++++++++++++++++++++++++++++++++++++++ ExplorerPatcher/utility.h | 2 + 3 files changed, 118 insertions(+), 5 deletions(-) diff --git a/ExplorerPatcher/dllmain.c b/ExplorerPatcher/dllmain.c index b85e9ae..5f9448f 100644 --- a/ExplorerPatcher/dllmain.c +++ b/ExplorerPatcher/dllmain.c @@ -3964,13 +3964,36 @@ SIZE WINAPI PeopleButton_CalculateMinimumSizeHook(void* _this, SIZE* pSz) rcWeatherFlyoutWindow.top = mi.rcWork.top; rcWeatherFlyoutWindow.right = rcWeatherFlyoutWindow.left + MulDiv(MulDiv(EP_WEATHER_WIDTH, dpiX, 96), dwTextScaleFactor, 100); rcWeatherFlyoutWindow.bottom = rcWeatherFlyoutWindow.top + MulDiv(MulDiv(EP_WEATHER_HEIGHT, dpiX, 96), dwTextScaleFactor, 100); - if (FAILED(epw->lpVtbl->Initialize(epw, wszEPWeatherKillswitch, bAllocConsole, EP_WEATHER_PROVIDER_GOOGLE, rt, rt, dwWeatherTemperatureUnit, dwWeatherUpdateSchedule * 1000, rcWeatherFlyoutWindow, dwWeatherTheme, dwWeatherGeolocationMode, &hWndWeatherFlyout))) + int k = 0; + while (FAILED(hr = epw->lpVtbl->Initialize(epw, wszEPWeatherKillswitch, bAllocConsole, EP_WEATHER_PROVIDER_GOOGLE, rt, rt, dwWeatherTemperatureUnit, dwWeatherUpdateSchedule * 1000, rcWeatherFlyoutWindow, dwWeatherTheme, dwWeatherGeolocationMode, &hWndWeatherFlyout))) { - epw->lpVtbl->Release(epw); - epw = NULL; - prev_total_h = 0; + BOOL bFailed = FALSE; + if (k == 0 && hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) + { + if (DownloadAndInstallWebView2Runtime()) + { + k++; + } + else + { + bFailed = TRUE; + } + } + else + { + bFailed = TRUE; + } + if (bFailed) + { + epw->lpVtbl->Release(epw); + epw = NULL; + prev_total_h = 0; + PostMessageW(FindWindowW(L"Shell_TrayWnd", NULL), WM_COMMAND, 435, 0); + PostMessageW(FindWindowW(L"ExplorerPatcher_GUI_" _T(EP_CLSID), NULL), WM_USER + 1, 0, 0); + break; + } } - else + if (SUCCEEDED(hr)) { epw->lpVtbl->SetWindowCornerPreference(epw, dwWeatherWindowCornerPreference); } diff --git a/ExplorerPatcher/utility.c b/ExplorerPatcher/utility.c index 5ce7b71..b0ac62d 100644 --- a/ExplorerPatcher/utility.c +++ b/ExplorerPatcher/utility.c @@ -1,4 +1,6 @@ #include "utility.h" +#include +#pragma comment(lib, "Wininet.lib") #pragma region "Weird stuff" INT64 STDMETHODCALLTYPE nimpl4_1(INT64 a1, DWORD* a2) @@ -1279,4 +1281,90 @@ LRESULT CALLBACK PleaseWait_HookProc(int code, WPARAM wParam, LPARAM lParam) PleaseWaitHook = NULL; } return result; + +BOOL DownloadAndInstallWebView2Runtime() +{ + BOOL bOK = FALSE; + HINTERNET hInternet = NULL; + if (hInternet = InternetOpenA( + L"ExplorerPatcher", + INTERNET_OPEN_TYPE_PRECONFIG, + NULL, + NULL, + 0 + )) + { + HINTERNET hConnect = InternetOpenUrlA( + hInternet, + "https://go.microsoft.com/fwlink/p/?LinkId=2124703", + NULL, + 0, + INTERNET_FLAG_RAW_DATA | + INTERNET_FLAG_RELOAD | + INTERNET_FLAG_RESYNCHRONIZE | + INTERNET_FLAG_NO_COOKIES | + INTERNET_FLAG_NO_UI | + INTERNET_FLAG_NO_CACHE_WRITE | + INTERNET_FLAG_DONT_CACHE, + NULL + ); + if (hConnect) + { + char* exe_buffer = NULL; + DWORD dwSize = 2 * 1024 * 1024; + DWORD dwRead = dwSize; + exe_buffer = calloc(dwSize, sizeof(char)); + if (exe_buffer) + { + BOOL bRet = FALSE; + if (bRet = InternetReadFile( + hConnect, + exe_buffer, + dwSize - 1, + &dwRead + )) + { + WCHAR wszPath[MAX_PATH]; + ZeroMemory(wszPath, MAX_PATH * sizeof(WCHAR)); + SHGetFolderPathW(NULL, SPECIAL_FOLDER_LEGACY, NULL, SHGFP_TYPE_CURRENT, wszPath); + wcscat_s(wszPath, MAX_PATH, _T(APP_RELATIVE_PATH)); + BOOL bRet = CreateDirectoryW(wszPath, NULL); + if (bRet || (!bRet && GetLastError() == ERROR_ALREADY_EXISTS)) + { + wcscat_s(wszPath, MAX_PATH, L"\\MicrosoftEdgeWebview2Setup.exe"); + FILE* f = NULL; + _wfopen_s(&f, wszPath, L"wb"); + if (f) + { + fwrite(exe_buffer, 1, dwRead, f); + fclose(f); + SHELLEXECUTEINFOW sei; + ZeroMemory(&sei, sizeof(SHELLEXECUTEINFOW)); + sei.cbSize = sizeof(sei); + sei.fMask = SEE_MASK_NOCLOSEPROCESS; + sei.hwnd = NULL; + sei.hInstApp = NULL; + sei.lpVerb = NULL; + sei.lpFile = wszPath; + sei.lpParameters = L""; + sei.hwnd = NULL; + sei.nShow = SW_SHOWMINIMIZED; + if (ShellExecuteExW(&sei) && sei.hProcess) + { + WaitForSingleObject(sei.hProcess, INFINITE); + CloseHandle(sei.hProcess); + Sleep(100); + DeleteFileW(wszPath); + bOK = TRUE; + } + } + } + } + free(exe_buffer); + } + InternetCloseHandle(hConnect); + } + InternetCloseHandle(hInternet); + } + return bOK; } \ No newline at end of file diff --git a/ExplorerPatcher/utility.h b/ExplorerPatcher/utility.h index 77203d6..075dc8e 100644 --- a/ExplorerPatcher/utility.h +++ b/ExplorerPatcher/utility.h @@ -562,4 +562,6 @@ extern BOOL (*PleaseWaitCallbackFunc)(void* data); BOOL PleaseWait_UpdateTimeout(int timeout); VOID CALLBACK PleaseWait_TimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime); LRESULT CALLBACK PleaseWait_HookProc(int code, WPARAM wParam, LPARAM lParam); + +BOOL DownloadAndInstallWebView2Runtime(); #endif