From 7c3f24b20d43f510a13fad35fa574d2624a43537 Mon Sep 17 00:00:00 2001 From: zilmar Date: Fri, 6 Nov 2015 22:55:21 +1100 Subject: [PATCH] [common] Add util class --- Source/Common/Common.vcproj | 8 ++++ Source/Common/Common.vcxproj | 2 + Source/Common/Common.vcxproj.filters | 6 +++ Source/Common/Util.cpp | 61 ++++++++++++++++++++++++++++ Source/Common/Util.h | 14 +++++++ 5 files changed, 91 insertions(+) create mode 100644 Source/Common/Util.cpp create mode 100644 Source/Common/Util.h diff --git a/Source/Common/Common.vcproj b/Source/Common/Common.vcproj index 651bcd98d..a93bb74a9 100644 --- a/Source/Common/Common.vcproj +++ b/Source/Common/Common.vcproj @@ -185,6 +185,10 @@ RelativePath="Trace.cpp" > + + + + diff --git a/Source/Common/Common.vcxproj b/Source/Common/Common.vcxproj index 3da6fdc3a..0a2d5b051 100644 --- a/Source/Common/Common.vcxproj +++ b/Source/Common/Common.vcxproj @@ -45,6 +45,7 @@ + @@ -61,5 +62,6 @@ + \ No newline at end of file diff --git a/Source/Common/Common.vcxproj.filters b/Source/Common/Common.vcxproj.filters index 4c71fc098..1f3aa4e29 100644 --- a/Source/Common/Common.vcxproj.filters +++ b/Source/Common/Common.vcxproj.filters @@ -44,6 +44,9 @@ Source Files + + Source Files + @@ -88,5 +91,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Source/Common/Util.cpp b/Source/Common/Util.cpp new file mode 100644 index 000000000..ab32891a2 --- /dev/null +++ b/Source/Common/Util.cpp @@ -0,0 +1,61 @@ +#include "stdafx.h" +#include "Util.h" +#include + +void pjutil::Sleep(uint32_t timeout) +{ + ::Sleep(timeout); +} + +bool pjutil::TerminatedExistingExe() +{ + bool bTerminated = false; + bool AskedUser = false; + DWORD pid = GetCurrentProcessId(); + + HANDLE nSearch = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (nSearch != INVALID_HANDLE_VALUE) + { + PROCESSENTRY32 lppe; + + memset(&lppe, 0, sizeof(PROCESSENTRY32)); + lppe.dwSize = sizeof(PROCESSENTRY32); + stdstr ModuleName = CPath(CPath::MODULE_FILE).GetNameExtension(); + + if (Process32First(nSearch, &lppe)) + { + do + { + if (_stricmp(lppe.szExeFile, ModuleName.c_str()) != 0 || + lppe.th32ProcessID == pid) + { + continue; + } + if (!AskedUser) + { + AskedUser = true; + int res = MessageBox(NULL, stdstr_f("%s currently running\n\nTerminate pid %d now?", ModuleName.c_str(), lppe.th32ProcessID).c_str(), stdstr_f("Terminate %s",ModuleName.c_str()).c_str(), MB_YESNO | MB_ICONEXCLAMATION); + if (res != IDYES) + { + break; + } + } + HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lppe.th32ProcessID); + if (hHandle != NULL) + { + if (TerminateProcess(hHandle, 0)) + { + bTerminated = true; + } + else + { + MessageBox(NULL, stdstr_f("Failed to terminate pid %d", lppe.th32ProcessID).c_str(), stdstr_f("Terminate %s failed!",ModuleName.c_str()).c_str(), MB_YESNO | MB_ICONEXCLAMATION); + } + CloseHandle(hHandle); + } + } while (Process32Next(nSearch, &lppe)); + } + CloseHandle(nSearch); + } + return bTerminated; +} \ No newline at end of file diff --git a/Source/Common/Util.h b/Source/Common/Util.h new file mode 100644 index 000000000..4dc0a48a1 --- /dev/null +++ b/Source/Common/Util.h @@ -0,0 +1,14 @@ +#pragma once +#include "stdtypes.h" + +class pjutil +{ +public: + static void Sleep(uint32_t timeout); + static bool TerminatedExistingExe(); + +private: + pjutil(void); // Disable default constructor + pjutil(const pjutil&); // Disable copy constructor + pjutil& operator=(const pjutil&); // Disable assignment +}; \ No newline at end of file