-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskManager.cpp
93 lines (64 loc) · 1.75 KB
/
TaskManager.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
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
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <psapi.h>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void PrintProcessNameAndID(DWORD processID)
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
// Get a handle to the process.
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID);
// Get the process name.
if (NULL != hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
&cbNeeded))
{
GetModuleBaseName(hProcess, hMod, szProcessName,
sizeof(szProcessName) / sizeof(TCHAR));
}
}
// Print the process name and identifier.
_tprintf(TEXT("%s (PID: %u)\n"), szProcessName, processID);
// Release the handle to the process.
CloseHandle(hProcess);
}
int main(void)
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return 1;
}
cProcesses = cbNeeded / sizeof(DWORD);
for (i = 0; i < cProcesses; i++)
{
if (aProcesses[i] != 0)
{
PrintProcessNameAndID(aProcesses[i]);
}
}
ofstream myfile;
myfile.open("Log.txt"); //Creates a txt file with the process list
myfile << aProcesses;
cout << "\n Press ENTER to continue\n";
string close;
getline(std::cin, close);
cout << " Write the name of the Process(default extension is .exe)\n";
cout << " Type 'exit' to finish the execution\n";
string name;
cin >> name;
if (name =="exit") { system("taskkill /f /im ProcessKiller.exe"); }
else {
system(("taskkill /f /im " + name + ".exe").c_str()); //Kills the process by its name
}
return 0;
}