From dcbb0f5db55566d3d38e925e4776df7dded111e7 Mon Sep 17 00:00:00 2001 From: luiscuenca Date: Wed, 3 Jul 2019 15:22:41 -0700 Subject: [PATCH] Shut down interface when executing the Windows Launcher --- launchers/win32/LauncherApp.cpp | 5 +++++ launchers/win32/LauncherDlg.cpp | 6 ++++-- launchers/win32/LauncherManager.cpp | 17 +++++++++-------- launchers/win32/LauncherUtils.cpp | 15 ++++++++++++++- launchers/win32/LauncherUtils.h | 3 ++- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/launchers/win32/LauncherApp.cpp b/launchers/win32/LauncherApp.cpp index 244d618fcb..a432a0b116 100644 --- a/launchers/win32/LauncherApp.cpp +++ b/launchers/win32/LauncherApp.cpp @@ -32,6 +32,11 @@ CLauncherApp theApp; // CLauncherApp initialization BOOL CLauncherApp::InitInstance() { + // Close interface if is running + int interfacePID = -1; + if (LauncherUtils::IsProcessRunning(L"interface.exe", interfacePID)) { + LauncherUtils::shutdownProcess(interfacePID, 0); + } int iNumOfArgs; LPWSTR* pArgs = CommandLineToArgvW(GetCommandLine(), &iNumOfArgs); bool isUninstalling = false; diff --git a/launchers/win32/LauncherDlg.cpp b/launchers/win32/LauncherDlg.cpp index 6016424563..83af659208 100644 --- a/launchers/win32/LauncherDlg.cpp +++ b/launchers/win32/LauncherDlg.cpp @@ -629,7 +629,8 @@ void CLauncherDlg::OnTimer(UINT_PTR nIDEvent) { ::SetForegroundWindow(_applicationWND); ::SetActiveWindow(_applicationWND); } - if (LauncherUtils::IsProcessRunning(L"interface.exe")) { + int interfacePID = -1; + if (LauncherUtils::IsProcessRunning(L"interface.exe", interfacePID)) { exit(0); } } @@ -653,7 +654,8 @@ void CLauncherDlg::OnTimer(UINT_PTR nIDEvent) { } _splashStep++; } else if (theApp._manager.shouldShutDown()) { - if (LauncherUtils::IsProcessRunning(L"interface.exe")) { + int interfacePID = -1; + if (LauncherUtils::IsProcessRunning(L"interface.exe", interfacePID)) { exit(0); } } diff --git a/launchers/win32/LauncherManager.cpp b/launchers/win32/LauncherManager.cpp index 2916f614cb..619ebe9c42 100644 --- a/launchers/win32/LauncherManager.cpp +++ b/launchers/win32/LauncherManager.cpp @@ -15,13 +15,10 @@ #include "LauncherManager.h" -LauncherManager::LauncherManager() -{ +LauncherManager::LauncherManager() { } - -LauncherManager::~LauncherManager() -{ +LauncherManager::~LauncherManager() { } void LauncherManager::init() { @@ -113,8 +110,11 @@ BOOL LauncherManager::installLauncher() { // The installer is not running on the desired location and has to be installed // Kill of running before self-copy addToLog(_T("Installing Launcher.")); - if (LauncherUtils::IsProcessRunning(LAUNCHER_EXE_FILENAME)) { - ShellExecute(NULL, NULL, L"taskkill", L"/F /T /IM " + LAUNCHER_EXE_FILENAME, NULL, SW_HIDE); + int launcherPID = -1; + if (LauncherUtils::IsProcessRunning(LAUNCHER_EXE_FILENAME, launcherPID)) { + if (!LauncherUtils::shutdownProcess(launcherPID, 0)) { + addToLog(_T("Error shutting down the Launcher")); + } } CopyFile(appPath, instalationPath, FALSE); } @@ -308,7 +308,8 @@ LauncherUtils::ResponseError LauncherManager::readConfigJSON(CString& version, C } Json::Value config; configFile >> config; - if (config["version"].isString() && config["domain"].isString() && + if (config["version"].isString() && + config["domain"].isString() && config["content"].isString()) { loggedIn = config["loggedIn"].asBool(); version = config["version"].asCString(); diff --git a/launchers/win32/LauncherUtils.cpp b/launchers/win32/LauncherUtils.cpp index 9365dd52a1..39964cc457 100644 --- a/launchers/win32/LauncherUtils.cpp +++ b/launchers/win32/LauncherUtils.cpp @@ -37,7 +37,19 @@ CString LauncherUtils::urlEncodeString(const CString& url) { return stringOut; } -BOOL LauncherUtils::IsProcessRunning(const wchar_t *processName) { +BOOL LauncherUtils::shutdownProcess(DWORD dwProcessId, UINT uExitCode) { + DWORD dwDesiredAccess = PROCESS_TERMINATE; + BOOL bInheritHandle = FALSE; + HANDLE hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); + if (hProcess == NULL) { + return FALSE; + } + BOOL result = TerminateProcess(hProcess, uExitCode); + CloseHandle(hProcess); + return result; +} + +BOOL LauncherUtils::IsProcessRunning(const wchar_t *processName, int& processID) { bool exists = false; PROCESSENTRY32 entry; entry.dwSize = sizeof(PROCESSENTRY32); @@ -48,6 +60,7 @@ BOOL LauncherUtils::IsProcessRunning(const wchar_t *processName) { while (Process32Next(snapshot, &entry)) { if (!_wcsicmp(entry.szExeFile, processName)) { exists = true; + processID = entry.th32ProcessID; break; } } diff --git a/launchers/win32/LauncherUtils.h b/launchers/win32/LauncherUtils.h index a6f124e18d..f5ce04b386 100644 --- a/launchers/win32/LauncherUtils.h +++ b/launchers/win32/LauncherUtils.h @@ -73,7 +73,8 @@ public: static std::string cStringToStd(CString cstring); static BOOL getFont(const CString& fontName, int fontSize, bool isBold, CFont& fontOut); static BOOL launchApplication(LPCWSTR lpApplicationName, LPTSTR cmdArgs = _T("")); - static BOOL IsProcessRunning(const wchar_t *processName); + static BOOL IsProcessRunning(const wchar_t *processName, int& processID); + static BOOL shutdownProcess(DWORD dwProcessId, UINT uExitCode); static BOOL insertRegistryKey(const std::string& regPath, const std::string& name, const std::string& value); static BOOL insertRegistryKey(const std::string& regPath, const std::string& name, DWORD value); static BOOL deleteFileOrDirectory(const CString& dirPath, bool noRecycleBin = true);