[common] Add util class
This commit is contained in:
parent
19bf3e9945
commit
7c3f24b20d
|
@ -185,6 +185,10 @@
|
||||||
RelativePath="Trace.cpp"
|
RelativePath="Trace.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\Util.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
</Filter>
|
</Filter>
|
||||||
<Filter
|
<Filter
|
||||||
Name="Header Files"
|
Name="Header Files"
|
||||||
|
@ -246,6 +250,10 @@
|
||||||
RelativePath="TraceDefs.h"
|
RelativePath="TraceDefs.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\Util.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
</Filter>
|
</Filter>
|
||||||
</Files>
|
</Files>
|
||||||
<Globals>
|
<Globals>
|
||||||
|
|
|
@ -45,6 +45,7 @@
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="SyncEvent.cpp" />
|
<ClCompile Include="SyncEvent.cpp" />
|
||||||
<ClCompile Include="Trace.cpp" />
|
<ClCompile Include="Trace.cpp" />
|
||||||
|
<ClCompile Include="Util.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="CriticalSection.h" />
|
<ClInclude Include="CriticalSection.h" />
|
||||||
|
@ -61,5 +62,6 @@
|
||||||
<ClInclude Include="SyncEvent.h" />
|
<ClInclude Include="SyncEvent.h" />
|
||||||
<ClInclude Include="Trace.h" />
|
<ClInclude Include="Trace.h" />
|
||||||
<ClInclude Include="TraceDefs.h" />
|
<ClInclude Include="TraceDefs.h" />
|
||||||
|
<ClInclude Include="Util.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -44,6 +44,9 @@
|
||||||
<ClCompile Include="SyncEvent.cpp">
|
<ClCompile Include="SyncEvent.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Util.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="CriticalSection.h">
|
<ClInclude Include="CriticalSection.h">
|
||||||
|
@ -88,5 +91,8 @@
|
||||||
<ClInclude Include="SyncEvent.h">
|
<ClInclude Include="SyncEvent.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Util.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -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;
|
||||||
|
}
|
|
@ -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
|
||||||
|
};
|
Loading…
Reference in New Issue