فهرست منبع

updater: Pass AppData path to elevated process

derrod 3 سال پیش
والد
کامیت
778cf5a96a
1فایلهای تغییر یافته به همراه30 افزوده شده و 23 حذف شده
  1. 30 23
      UI/win-update/updater/updater.cpp

+ 30 - 23
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<wchar_t> 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<wchar_t> 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<wchar_t> 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;