From 4be5fbc996a104b1ec99327a2e46f45d1aaeb58f Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 27 Aug 2020 21:26:06 +0300 Subject: Plugin Updater: more code cleaning --- plugins/PluginUpdater/pu_stub/src/pu_stub.cpp | 12 +++-- plugins/PluginUpdater/src/Utils.cpp | 66 +++++++++++++-------------- 2 files changed, 41 insertions(+), 37 deletions(-) (limited to 'plugins/PluginUpdater') diff --git a/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp b/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp index cca42b5c10..f93a07484d 100644 --- a/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp +++ b/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp @@ -85,13 +85,15 @@ int APIENTRY wWinMain(HINSTANCE /*hInstance*/, HINSTANCE, LPTSTR lpCmdLine, int) case 2: // move if (!DeleteFileW(ptszFile2)) { - dwError = GetLastError(); - if (dwError != ERROR_ACCESS_DENIED && dwError != ERROR_FILE_NOT_FOUND) + DWORD err = GetLastError(); + if (err != ERROR_ACCESS_DENIED && err != ERROR_FILE_NOT_FOUND) { + dwError = err; break; + } } if (!MoveFileW(ptszFile1, ptszFile2)) { // use copy on error - switch (dwError = GetLastError()) { + switch (DWORD err = GetLastError()) { case ERROR_ALREADY_EXISTS: dwError = 0; break; // this file was included into many archives, so Miranda tries to move it again & again @@ -106,8 +108,10 @@ int APIENTRY wWinMain(HINSTANCE /*hInstance*/, HINSTANCE, LPTSTR lpCmdLine, int) if (!DeleteFileW(ptszFile1)) dwError = GetLastError(); + break; - dwError = 0; + default: + dwError = err; break; } } diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp index 5121ab8df3..33acef4b87 100644 --- a/plugins/PluginUpdater/src/Utils.cpp +++ b/plugins/PluginUpdater/src/Utils.cpp @@ -389,6 +389,7 @@ bool PrepareEscalation() if (ext != nullptr) *ext = '\0'; wcscat(szPath, L".test"); + HANDLE hFile = CreateFile(szPath, GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (hFile != INVALID_HANDLE_VALUE) { // we are admins or UAC is disable, cool @@ -396,46 +397,45 @@ bool PrepareEscalation() DeleteFile(szPath); return true; } - else if (IsRunAsAdmin()) { - // Check the current process's "run as administrator" status. + + // Check the current process's "run as administrator" status. + if (IsRunAsAdmin()) return true; + + // Elevate the process. Create a pipe for a stub first + wchar_t tszPipeName[MAX_PATH]; + mir_snwprintf(tszPipeName, L"\\\\.\\pipe\\Miranda_Pu_%d", GetCurrentProcessId()); + hPipe = CreateNamedPipe(tszPipeName, PIPE_ACCESS_DUPLEX, PIPE_READMODE_BYTE | PIPE_WAIT, 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, nullptr); + if (hPipe == INVALID_HANDLE_VALUE) { + hPipe = nullptr; } else { - // Elevate the process. Create a pipe for a stub first - wchar_t tszPipeName[MAX_PATH]; - mir_snwprintf(tszPipeName, L"\\\\.\\pipe\\Miranda_Pu_%d", GetCurrentProcessId()); - hPipe = CreateNamedPipe(tszPipeName, PIPE_ACCESS_DUPLEX, PIPE_READMODE_BYTE | PIPE_WAIT, 1, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, nullptr); - if (hPipe == INVALID_HANDLE_VALUE) { - hPipe = nullptr; + wchar_t cmdLine[100], *p; + GetModuleFileName(nullptr, szPath, ARRAYSIZE(szPath)); + if ((p = wcsrchr(szPath, '\\')) != nullptr) + wcscpy(p + 1, L"pu_stub.exe"); + mir_snwprintf(cmdLine, L"%d", GetCurrentProcessId()); + + // Launch a stub + SHELLEXECUTEINFO sei = { sizeof(sei) }; + sei.lpVerb = L"runas"; + sei.lpFile = szPath; + sei.lpParameters = cmdLine; + sei.hwnd = nullptr; + sei.nShow = SW_NORMAL; + if (ShellExecuteEx(&sei)) { + if (hPipe != nullptr) + ConnectNamedPipe(hPipe, nullptr); + return true; } - else { - wchar_t cmdLine[100], *p; - GetModuleFileName(nullptr, szPath, ARRAYSIZE(szPath)); - if ((p = wcsrchr(szPath, '\\')) != nullptr) - wcscpy(p + 1, L"pu_stub.exe"); - mir_snwprintf(cmdLine, L"%d", GetCurrentProcessId()); - - // Launch a stub - SHELLEXECUTEINFO sei = { sizeof(sei) }; - sei.lpVerb = L"runas"; - sei.lpFile = szPath; - sei.lpParameters = cmdLine; - sei.hwnd = nullptr; - sei.nShow = SW_NORMAL; - if (ShellExecuteEx(&sei)) { - if (hPipe != nullptr) - ConnectNamedPipe(hPipe, nullptr); - return true; - } - DWORD dwError = GetLastError(); - if (dwError == ERROR_CANCELLED) { - // The user refused to allow privileges elevation. - // Do nothing ... - } + DWORD dwError = GetLastError(); + if (dwError == ERROR_CANCELLED) { + // The user refused to allow privileges elevation. + // Do nothing ... } - return false; } + return false; } ///////////////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3