[common] Add util class

This commit is contained in:
zilmar 2015-11-06 22:55:21 +11:00
parent 19bf3e9945
commit 7c3f24b20d
5 changed files with 91 additions and 0 deletions

View File

@ -185,6 +185,10 @@
RelativePath="Trace.cpp"
>
</File>
<File
RelativePath=".\Util.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
@ -246,6 +250,10 @@
RelativePath="TraceDefs.h"
>
</File>
<File
RelativePath=".\Util.h"
>
</File>
</Filter>
</Files>
<Globals>

View File

@ -45,6 +45,7 @@
</ClCompile>
<ClCompile Include="SyncEvent.cpp" />
<ClCompile Include="Trace.cpp" />
<ClCompile Include="Util.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CriticalSection.h" />
@ -61,5 +62,6 @@
<ClInclude Include="SyncEvent.h" />
<ClInclude Include="Trace.h" />
<ClInclude Include="TraceDefs.h" />
<ClInclude Include="Util.h" />
</ItemGroup>
</Project>

View File

@ -44,6 +44,9 @@
<ClCompile Include="SyncEvent.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Util.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CriticalSection.h">
@ -88,5 +91,8 @@
<ClInclude Include="SyncEvent.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Util.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

61
Source/Common/Util.cpp Normal file
View File

@ -0,0 +1,61 @@
#include "stdafx.h"
#include "Util.h"
#include <Tlhelp32.h>
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;
}

14
Source/Common/Util.h Normal file
View File

@ -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
};