-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocess.cpp
More file actions
150 lines (120 loc) · 2.66 KB
/
process.cpp
File metadata and controls
150 lines (120 loc) · 2.66 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "StdAfx.h"
#include "process.h"
Process::Process()
{
pe32.dwSize = sizeof(PROCESSENTRY32);
}
bool Process::init()
{
/*
* @msdn:
* TH32CS_SNAPPROCESS : Includes all processes in the system in the snapshot.
* To enumerate the processes, see Process32First.
*/
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapShot == INVALID_HANDLE_VALUE)
{
return false;
}
if (Process32First(hSnapShot, &pe32) == false)
{
if (hSnapShot != INVALID_HANDLE_VALUE) {
CloseHandle(hSnapShot);
}
return false;
}
return true;
}
wchar_t *Process::getProcess()
{
if (Process32Next(hSnapShot, &pe32) == false)
{
if (hSnapShot != INVALID_HANDLE_VALUE)
{
CloseHandle(hSnapShot);
}
return false;
}
return (pe32.szExeFile);
}
DWORD Process::getPidByName(wchar_t *procName)
{
wchar_t *current = NULL;
while ((current = getProcess()) != NULL)
{
if (!_tcsicmp(current, procName)) {
return (pe32.th32ProcessID);
}
}
return (PROCESS_NO_EXISTS);
}
DWORD Process::KillProcessById(DWORD pid)
{
HANDLE hProcess;
hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid);
if (hProcess == ERROR_SUCCESS)
{
CloseHandle(hProcess);
return (false);
}
if (TerminateProcess(hProcess, 0) == 0)
{
CloseHandle(hProcess);
return false;
}
CloseHandle(hProcess);
return KILLING_SUCCESSFUL;
}
DWORD Process::KillProcessByName(wchar_t *procName)
{
DWORD pid;
DWORD result;
do {
pid = getPidByName(procName);
if (pid == PROCESS_NO_EXISTS)
break;
if (KillProcessById(pid) != KILLING_SUCCESSFUL)
result = ERROR_ALL_PROCESS_KILLED;
} while(pid != NULL);
return (result);
}
/****
Return 0 is the function successful
****/
DWORD Process::SetDebugPrivileges()
{
HANDLE hToken;
TOKEN_PRIVILEGES DBGPriv;
/****
@msdn :
The AdjustTokenPrivileges function enables or disables privileges
in the specified access token. Enabling or disabling privileges
in an access token requires TOKEN_ADJUST_PRIVILEGES access.
***/
if ((OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) == 0)
{
if (hToken != INVALID_HANDLE_VALUE)
{
CloseHandle(hToken);
}
return (GetLastError());
}
if ((LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &DBGPriv.Privileges[0].Luid) == 0))
{
CloseHandle(hToken);
return (GetLastError());
}
/****
@msdn :
PrivilegeCount : This must be set to the number of entries in the Privileges array.
****/
DBGPriv.PrivilegeCount = 1;
DBGPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (AdjustTokenPrivileges(hToken, false, &DBGPriv, 0, NULL, NULL) == ERROR_SUCCESS)
{
CloseHandle(hToken);
return (0);
}
CloseHandle(hToken);
return (GetLastError());
}