mirror of
https://github.com/obsproject/obs-studio.git
synced 2024-09-20 13:08:50 +02:00
UI/updater: Fix launching OBS as admin post-update
A long standing lack of synchronization on the elevated process handle made the non-elevated process always exit, so the updater would assume it was started elevated and launch OBS from the elevated context. Also applies the altered working directory to the non-elevated code path so it can find the OBS executable properly.
This commit is contained in:
parent
3e1b5426e3
commit
078ea5222d
@ -1563,16 +1563,13 @@ static INT_PTR CALLBACK UpdateDialogProc(HWND hwnd, UINT message, WPARAM wParam,
|
||||
return false;
|
||||
}
|
||||
|
||||
static void RestartAsAdmin(LPWSTR lpCmdLine)
|
||||
static int RestartAsAdmin(LPCWSTR lpCmdLine, LPCWSTR cwd)
|
||||
{
|
||||
wchar_t myPath[MAX_PATH];
|
||||
if (!GetModuleFileNameW(nullptr, myPath, _countof(myPath) - 1)) {
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
wchar_t cwd[MAX_PATH];
|
||||
GetCurrentDirectoryW(_countof(cwd) - 1, cwd);
|
||||
|
||||
SHELLEXECUTEINFO shExInfo = {0};
|
||||
shExInfo.cbSize = sizeof(shExInfo);
|
||||
shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
|
||||
@ -1591,13 +1588,16 @@ static void RestartAsAdmin(LPWSTR lpCmdLine)
|
||||
if (ShellExecuteEx(&shExInfo)) {
|
||||
DWORD exitCode;
|
||||
|
||||
WaitForSingleObject(shExInfo.hProcess, INFINITE);
|
||||
|
||||
if (GetExitCodeProcess(shExInfo.hProcess, &exitCode)) {
|
||||
if (exitCode == 1) {
|
||||
LaunchOBS();
|
||||
return exitCode;
|
||||
}
|
||||
}
|
||||
CloseHandle(shExInfo.hProcess);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool HasElevation()
|
||||
@ -1622,11 +1622,25 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int)
|
||||
{
|
||||
INITCOMMONCONTROLSEX icce;
|
||||
|
||||
wchar_t cwd[MAX_PATH];
|
||||
wchar_t newPath[MAX_PATH];
|
||||
GetCurrentDirectoryW(_countof(cwd) - 1, cwd);
|
||||
|
||||
is32bit = wcsstr(cwd, L"bin\\32bit") != nullptr;
|
||||
|
||||
if (!HasElevation()) {
|
||||
HANDLE hLowMutex = CreateMutexW(
|
||||
nullptr, true, L"OBSUpdaterRunningAsNonAdminUser");
|
||||
|
||||
RestartAsAdmin(lpCmdLine);
|
||||
/* return code 1 = user wanted to launch OBS */
|
||||
if (RestartAsAdmin(lpCmdLine, cwd) == 1) {
|
||||
StringCbCat(cwd, sizeof(cwd), L"\\..\\..");
|
||||
GetFullPathName(cwd, _countof(newPath), newPath,
|
||||
nullptr);
|
||||
SetCurrentDirectory(newPath);
|
||||
|
||||
LaunchOBS();
|
||||
}
|
||||
|
||||
if (hLowMutex) {
|
||||
ReleaseMutex(hLowMutex);
|
||||
@ -1635,18 +1649,9 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int)
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
{
|
||||
wchar_t cwd[MAX_PATH];
|
||||
wchar_t newPath[MAX_PATH];
|
||||
GetCurrentDirectoryW(_countof(cwd) - 1, cwd);
|
||||
|
||||
is32bit = wcsstr(cwd, L"bin\\32bit") != nullptr;
|
||||
StringCbCat(cwd, sizeof(cwd), L"\\..\\..");
|
||||
|
||||
GetFullPathName(cwd, _countof(newPath), newPath,
|
||||
nullptr);
|
||||
SetCurrentDirectory(newPath);
|
||||
}
|
||||
StringCbCat(cwd, sizeof(cwd), L"\\..\\..");
|
||||
GetFullPathName(cwd, _countof(newPath), newPath, nullptr);
|
||||
SetCurrentDirectory(newPath);
|
||||
|
||||
hinstMain = hInstance;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user