From 778cf5a96ab7bb268240af7bc448eee41006da97 Mon Sep 17 00:00:00 2001 From: derrod Date: Mon, 16 Jan 2023 02:29:10 +0100 Subject: [PATCH] updater: Pass AppData path to elevated process --- UI/win-update/updater/updater.cpp | 53 +++++++++++++++++-------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/UI/win-update/updater/updater.cpp b/UI/win-update/updater/updater.cpp index 795fd5ca7..9b78b2812 100644 --- a/UI/win-update/updater/updater.cpp +++ b/UI/win-update/updater/updater.cpp @@ -1352,6 +1352,7 @@ static bool Update(wchar_t *cmdLine) bool bIsPortable = false; wstring branch = L"stable"; + wstring appdata; if (cmdLine[0]) { int argc; @@ -1366,6 +1367,9 @@ static bool Update(wchar_t *cmdLine) } else if (wcsncmp(argv[i], L"--branch=", 9) == 0) { branch = argv[i] + 9; + } else if (wcsncmp(argv[i], L"--appdata=", + 10) == 0) { + appdata = argv[i] + 10; } else if (wcscmp(argv[i], L"--portable") == 0) { bIsPortable = true; @@ -1385,18 +1389,16 @@ static bool Update(wchar_t *cmdLine) GetCurrentDirectory(_countof(lpAppDataPath), lpAppDataPath); StringCbCat(lpAppDataPath, sizeof(lpAppDataPath), L"\\config"); } else { - DWORD ret; - ret = GetEnvironmentVariable(L"OBS_USER_APPDATA_PATH", - lpAppDataPath, - _countof(lpAppDataPath)); - - if (ret >= _countof(lpAppDataPath)) { - Status(L"Update failed: Could not determine AppData " - L"location"); - return false; - } - - if (!ret) { + if (!appdata.empty()) { + HRESULT hr = StringCbCopy(lpAppDataPath, + sizeof(lpAppDataPath), + appdata.c_str()); + if (hr != S_OK) { + Status(L"Update failed: Could not determine AppData " + L"location"); + return false; + } + } else { CoTaskMemPtr pOut; HRESULT hr = SHGetKnownFolderPath( FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, @@ -1916,13 +1918,26 @@ static int RestartAsAdmin(LPCWSTR lpCmdLine, LPCWSTR cwd) return 0; } + /* If the admin is a different user, add the path to the user's + * AppData to the command line so we can load the correct manifest. */ + wstring elevatedCmdLine(lpCmdLine); + CoTaskMemPtr pOut; + HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, + KF_FLAG_DEFAULT, nullptr, &pOut); + if (hr == S_OK) { + elevatedCmdLine += L" \"--appdata="; + elevatedCmdLine += pOut; + elevatedCmdLine += L"\""; + } + SHELLEXECUTEINFO shExInfo = {0}; shExInfo.cbSize = sizeof(shExInfo); shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS; shExInfo.hwnd = 0; - shExInfo.lpVerb = L"runas"; /* Operation to perform */ - shExInfo.lpFile = myPath; /* Application to start */ - shExInfo.lpParameters = lpCmdLine; /* Additional parameters */ + shExInfo.lpVerb = L"runas"; /* Operation to perform */ + shExInfo.lpFile = myPath; /* Application to start */ + shExInfo.lpParameters = + elevatedCmdLine.c_str(); /* Additional parameters */ shExInfo.lpDirectory = cwd; shExInfo.nShow = SW_NORMAL; shExInfo.hInstApp = 0; @@ -1931,14 +1946,6 @@ static int RestartAsAdmin(LPCWSTR lpCmdLine, LPCWSTR cwd) * windows :( */ AllowSetForegroundWindow(ASFW_ANY); - /* if the admin is a different user, save the path to the user's - * appdata so we can load the correct manifest */ - CoTaskMemPtr pOut; - HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, - KF_FLAG_DEFAULT, nullptr, &pOut); - if (hr == S_OK) - SetEnvironmentVariable(L"OBS_USER_APPDATA_PATH", pOut); - if (ShellExecuteEx(&shExInfo)) { DWORD exitCode;