-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.cpp
59 lines (46 loc) · 1.38 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "util.h"
#include <TlHelp32.h>
#include <iostream>
#include <sstream>
#include <filesystem>
int FindProcessId(const std::string& processName)
{
int pid = -1;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 process;
ZeroMemory(&process, sizeof(process));
process.dwSize = sizeof(process);
if (Process32First(snapshot, &process))
{
do
{
if (std::string(process.szExeFile) == processName)
{
pid = process.th32ProcessID;
break;
}
} while (Process32Next(snapshot, &process));
}
CloseHandle(snapshot);
return pid;
}
void WaitForCloseProcess(const std::string& processName)
{
int pid = FindProcessId(processName);
if (pid == -1)
return;
std::cout << "Found '" << processName << "' process. Waiting for closing..." << std::endl;
#ifdef _DEBUG
std::stringstream stream;
stream << "taskkill /F /T /IM " << processName;
int retval = system(stream.str().c_str());
std::cout << "Trying to kill process." << std::endl;
#endif
HANDLE hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
DWORD exitCode = 0;
while (hProc && (GetExitCodeProcess(hProc, &exitCode), exitCode == STILL_ACTIVE)) {
Sleep(1000);
}
if (hProc != NULL)
CloseHandle(hProc);
}