-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.h
More file actions
414 lines (382 loc) · 12.3 KB
/
process.h
File metadata and controls
414 lines (382 loc) · 12.3 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
PROCESS_INFORMATION pi[100];
STARTUPINFO si[100];
LPSTR cString[100];
HANDLE hHandless[100];
HANDLE hForeProcess;
int status[100];
int numProcess = 0; // number of Process
#define MAX_NAME_ENV 16383
#define MAX_VALUE_ENV 100000
void exec(string command);
string pwd()
{
string getCurrentDir = "";
char *buffer = _getcwd(NULL, 0);
int i = 0;
while(buffer[i] != '\0')
{
getCurrentDir += buffer[i];
i++;
}
return getCurrentDir;
}
void signal_callback_handler(int signum)
{
if (hForeProcess != NULL)
{
TerminateProcess(hForeProcess, 0);
hForeProcess = NULL;
}
//exit(1);
}
void killOneProcess(string s)
{
int id = atoi(s.c_str());
bool unKill = true;
for (int i = 1; i <= numProcess; ++i)
{
if (pi[i].dwProcessId == id)
{
TerminateProcess(pi[i].hProcess, 0);
CloseHandle(pi[i].hThread);
CloseHandle(pi[i].hProcess);
printf("Process %s killed\n", cString[i]);
for (int j = i; j < numProcess; ++j)
{
status[j] = status[j + 1];
pi[j] = pi[j + 1];
si[j] = si[j + 1];
cString[j] = cString[j + 1];
}
numProcess--;
unKill = false;
break;
}
}
if (unKill)
printf("Can't find process with this id = %d\n", id);
}
void killAllProcess()
{
for (int i = 1; i <= numProcess; ++i)
{
TerminateProcess(pi[i].hProcess, 0);
CloseHandle(pi[i].hThread);
CloseHandle(pi[i].hProcess);
}
cout << "All process killed ...\n";
numProcess = 0;
}
void stopProcess(string s)
{
int id = atoi(s.c_str());
bool unStop = true;
for (int i = 1; i <= numProcess; ++i)
{
if (pi[i].dwProcessId == id)
{
if (status[i] == 1)
{
status[i] = 0;
cout << "Stop " << cString[i] << " success\n";
SuspendThread(pi[i].hThread);
unStop = false;
break;
}
else
{
cout << "Process " << cString[i] << " is still stopping" << endl;
unStop = false;
break;
}
}
}
if (unStop)
printf("Can't find process with this id = %d\n", id);
}
void resumeProcess(string s)
{
int id = atoi(s.c_str());
bool unResume = true;
for (int i = 1; i <= numProcess; ++i)
{
if (pi[i].dwProcessId == id)
{
if (status[i] == 0)
{
status[i] = 1;
cout << "Process " << cString[i] << " is running again\n";
ResumeThread(pi[i].hThread);
unResume = false;
break;
}
else
{
cout << "Process " << cString[i] << " is still running" << endl;
unResume = false;
break;
}
}
}
if(unResume)
printf("Can't find process with this id = %d\n", id);
}
void openProcess(const string mode, const string &s)
{
void openProcessInForeGround(const string &s);
void openProcessInBackGround(const string &s);
if (mode.compare("-bg") == 0 || mode.compare("--background") == 0)
openProcessInBackGround(s);
else if (mode.compare("-f") == 0 || mode.compare("--foreground") == 0)
openProcessInForeGround(s);
else
cout << "Error when start process!" << endl;
}
void openProcessInForeGround(const string &s)
{
PROCESS_INFORMATION pi; // lpStartupInfo // lpProcessInformation
STARTUPINFO si = {sizeof(STARTUPINFO)}; // cpp string must be modified to use in c
LPSTR cString = strdup(s.c_str());
ZeroMemory(&si, sizeof(si)); // fill this block with zeros
si.cb = sizeof(si); // CreateProcess(cString, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
if (!CreateProcess(cString, // No module name (use command line)
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NEW_CONSOLE,
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("Opening process not successful!\n");
return;
}
else
hForeProcess = pi.hProcess;
WaitForSingleObject(pi.hProcess, INFINITE); // INFINITE // hProcess: The handle is used to specify the process in all functions that perform operations on the process object.
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
void openProcessInBackGround(const string &s)
{
void kill(string s);
++numProcess;
status[numProcess] = 1;
si[numProcess] = {sizeof(STARTUPINFO)}; // lpStartupInfo // lpProcessInformation
pi[numProcess]; // cpp string must be modified to use in c
ZeroMemory(&si[numProcess], sizeof(si[numProcess])); // fill this block with zeros
si[numProcess].cb = sizeof(si[numProcess]);
cString[numProcess] = strdup(s.c_str()); // CreateProcess(cString, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
if (!CreateProcess(cString[numProcess], // No module name (use command line)
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NEW_CONSOLE,
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si[numProcess], // Pointer to STARTUPINFO structure
&pi[numProcess]) // Pointer to PROCESS_INFORMATION structure
)
{
TerminateProcess(pi[numProcess].hProcess, 0);
CloseHandle(pi[numProcess].hThread);
CloseHandle(pi[numProcess].hProcess);
numProcess--;
printf("Opening process not successful!\n");
return;
}
}
void listOfCurrent()
{
char *buffer;
if ((buffer = _getcwd(NULL, 0)) == NULL)
perror("Error: ");
if (_chdir(buffer))
{
switch (errno)
{
case ENOENT:
cout << "No such file or directory.\n";
break;
case EINVAL:
cout << "Invalid buffer.\n";
break;
default:
cout << "Unknown error.\n";
break;
}
}
else
system("dir");
free(buffer);
}
void showListProcess()
{
//Track running process
printf("\n");
printf("---------------------------------------------------------------------------------------------------------------------------------\n");
printf(" Numbers IdProcess hProcess Status Name \n");
printf("---------------------------------------------------------------------------------------------------------------------------------\n");
for (int i = 1; i <= numProcess; ++i)
{
DWORD dwExitCode;
GetExitCodeProcess(pi[i].hProcess, &dwExitCode);
if (dwExitCode != 259)
{
TerminateProcess(pi[i].hProcess, 0);
CloseHandle(pi[i].hThread);
CloseHandle(pi[i].hProcess);
for (int j = i; j < numProcess; ++j)
{
status[j] = status[j + 1];
pi[j] = pi[j + 1];
si[j] = si[j + 1];
cString[j] = cString[j + 1];
}
numProcess--;
i--;
}
else
{
const char *a = (status[i] == 0) ? "stopping" : "Running ";
printf(" %-19d%-26d%-20p%s %s\n", i, pi[i].dwProcessId, pi[i].hProcess, a, cString[i]);
}
}
cout << "\n\n";
}
void cd(string s)
{
char dir[100];
LPSTR cString = strdup(s.c_str()); // pass your path in the function
int ch = chdir(cString); // if the change of directory was successful it will print successful otherwise it will print not successful
if (ch < 0)
{
openProcess("-bg", cString);
}
}
void createNewDir(string path)
{
char *pathDir = const_cast<char*>(path.c_str());
if(mkdir(pathDir) == -1)
cerr << " Error : " << strerror(errno) << endl;
else
cout << "Folder Created" << endl;
}
void deleteDir(string path)
{
char *pathDir = const_cast<char*>(path.c_str());
if(rmdir(pathDir) == -1)
cerr << " Error : " << strerror(errno) << endl;
else
cout << "Folder Deleted" << endl;
}
void read_env(string strENVName)
{
char *envname = const_cast<char*>(strENVName.c_str());
HKEY hkey;
BYTE value[MAX_VALUE_ENV] ;
DWORD valsize = sizeof(value) ;
RegOpenKeyEx(HKEY_CURRENT_USER, "Environment", 0, KEY_ALL_ACCESS, &hkey);
if (strENVName == "")
{
for (int i = 0; ; i ++)
{
TCHAR name[MAX_NAME_ENV];
DWORD namesz = MAX_NAME_ENV;
value[0] = '\0';
DWORD valsize = MAX_VALUE_ENV;
if (RegEnumValue(hkey, i, name, &namesz, NULL, NULL, value, &valsize) == 0)
cout << (i < 9 ? "0":"") << i + 1 <<". " << name << " = " << value << endl;
else
break;
}
}
else if (RegQueryValueEx(hkey, envname, NULL, NULL, value, &valsize ) == 0)
cout << "The value of "<< envname << " is: " << value << endl;
else
cout << "There no variables has the name " << envname << endl;
RegCloseKey(hkey);
}
void add_env(string strENVName, string strENVValue)
{
char *envname = const_cast<char*>(strENVName.c_str());
char *envvalue = const_cast<char*>(strENVValue.c_str());
cout << "The environment variable "<< envname<< " is added\n";
HKEY hkey;
static BYTE value[100000] ;
DWORD valsize = sizeof(value) ;
RegOpenKeyEx(HKEY_CURRENT_USER,"Environment", 0, KEY_ALL_ACCESS, &hkey);
if(RegQueryValueEx(hkey, envname, NULL, NULL, value, &valsize) == 0)
{
char *name = (char*) value;
strcat(name, ";");
strcat(name, envvalue);
RegSetValueEx(hkey,envname,0,REG_SZ,(BYTE*) name, strlen(name));
}
else
RegSetValueEx(hkey,envname,0,REG_SZ,(BYTE*) envvalue, strlen(envvalue));
RegCloseKey(hkey);
}
void del_env(string strENVName)
{
char *envname = const_cast<char*>(strENVName.c_str());
HKEY hkey;
RegOpenKeyEx(HKEY_CURRENT_USER,"Environment", 0, KEY_ALL_ACCESS, &hkey);
if (RegDeleteValue(hkey, envname) == 0)
cout << "The environment variable " << envname << " is deleted" << endl;
else
cout << "There no variables has the name " << envname << endl;
RegCloseKey(hkey);
}
string getDateTimeNow()
{
while(1)
{
// Get local time
SYSTEMTIME st;
GetLocalTime(&st);
// Convert to tm struct
tm timeinfo;
timeinfo.tm_year = st.wYear - 1900;
timeinfo.tm_mon = st.wMonth - 1;
timeinfo.tm_mday = st.wDay;
timeinfo.tm_hour = st.wHour;
timeinfo.tm_min = st.wMinute;
timeinfo.tm_sec = st.wSecond;
// Format the date time string
ostringstream oss;
oss << put_time(&timeinfo, "%A %I:%M:%S %p %d/%m/%Y");
cout << '\r';
cout << oss.str();
this_thread::sleep_for(chrono::milliseconds(1000));
}
}
void getTimeNow()
{
while(1)
{
SYSTEMTIME currentTime;
GetLocalTime(¤tTime);
char timeString[20];
sprintf_s(timeString, "%02d:%02d:%02d", currentTime.wHour, currentTime.wMinute, currentTime.wSecond);
cout << '\r';
cout << string(timeString);
this_thread::sleep_for(chrono::milliseconds(1000));
}
}
void runBat(string path)
{
ifstream file(path);
if (file.is_open())
{
string line;
while (getline(file, line))
exec(line);
}
else
cout << "File " << path << " do not exist in this directory\n";
}