forked from maximmenshikov/common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.hpp
More file actions
126 lines (112 loc) · 2.74 KB
/
Copy pathProcess.hpp
File metadata and controls
126 lines (112 loc) · 2.74 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
#ifndef PROCESS_HPP
#define PROCESS_HPP
#include <tlhelp32.h>
#pragma comment(lib,"toolhelp.lib")
#include "ProcessFunctions_Common.h"
namespace Diagnostics
{
class Process
{
public:
static HANDLE Start(wchar_t *processName, wchar_t *arguments, wchar_t *accountName = NULL, BOOL autoCloseHandle = true)
{
if (wcslen(processName) > 0)
{
CE_PROCESS_INFORMATION cpi;;
memset(&cpi, 0, sizeof(cpi));
cpi.cbSize = sizeof(cpi);
if (accountName && wcsicmp(accountName, L"standard") == 0)
accountName = NULL;
cpi.lpwszAccountName = accountName;
if (CeCreateProcessEx(processName, arguments, &cpi) == TRUE)
{
if (cpi.ProcInfo.hThread != INVALID_HANDLE_VALUE)
CloseHandle(cpi.ProcInfo.hThread);
if (cpi.ProcInfo.hProcess != INVALID_HANDLE_VALUE)
{
if (autoCloseHandle)
CloseHandle(cpi.ProcInfo.hProcess);
return cpi.ProcInfo.hProcess;
}
}
}
return INVALID_HANDLE_VALUE;
}
static bool IsRunning(wchar_t *processname)
{
HANDLE prss = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS,0);
bool result = false;
PROCESSENTRY32 pre;
memset(&pre, 0, sizeof(pre));
pre.dwSize = sizeof(pre);
if (Process32First(prss, &pre))
{
do
{
if (wcsicmp(pre.szExeFile, processname) == 0)
{
result = true;
break;
}
} while (Process32Next(prss, &pre));
}
CloseToolhelp32Snapshot(prss);
return result;
}
static bool TerminateIfRunning(wchar_t *processname)
{
HANDLE prss = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS,0);
bool result = false;
PROCESSENTRY32 pre;
memset(&pre, 0, sizeof(pre));
pre.dwSize = sizeof(pre);
if (Process32First(prss, &pre))
{
do
{
if (wcsicmp(pre.szExeFile, processname) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pre.th32ProcessID);
if (hProcess)
{
if (!TerminateProcess(hProcess, 0))
{
CloseHandle(hProcess);
break;
}
result = true;
}
break;
}
} while (Process32Next(prss, &pre));
}
CloseToolhelp32Snapshot(prss);
return result;
}
static DWORD GetProcessID(wchar_t *processName)
{
if (processName == NULL)
return 0;
HANDLE prss = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS,0);
DWORD result = 0;
PROCESSENTRY32 pre;
memset(&pre, 0, sizeof(pre));
pre.dwSize = sizeof(pre);
if (Process32First(prss, &pre))
{
do
{
if (wcsicmp(pre.szExeFile, processName) == 0)
{
result = pre.th32ProcessID;
break;
}
} while (Process32Next(prss, &pre));
}
CloseToolhelp32Snapshot(prss);
return result;
}
};
}
#endif