-
Notifications
You must be signed in to change notification settings - Fork 0
/
processfunctions.h
170 lines (153 loc) · 5.45 KB
/
processfunctions.h
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
#ifndef PROCESSFUNCTIONS_H_INCLUDED
#define PROCESSFUNCTIONS_H_INCLUDED
#include <iostream> // cout, cin, etc
#include <stdio.h> // printf
#include <windows.h> // Import to use psapi
#include "gms.h" // GameMaker specific functionality
#include <psapi.h> // Depends on windows.h
#include <list> // List include
#include <string> // Strings
#include "myconfig.h"// types
/**
* @brief Writes all processIDs to Std List
*/
DWORD PSAPI_EnumProcesses(std::list<DWORD>& listProcessIDs, DWORD dwMaxProcessCount)
{
DWORD dwRet = NO_ERROR; // Set default ret to false
listProcessIDs.clear(); // Clear list in case there is still something in there
DWORD *pProcessIds = new DWORD[dwMaxProcessCount]; // Array to write the PIDs into
DWORD cb = dwMaxProcessCount * sizeof(DWORD); // The max bytesize
DWORD dwBytesReturned = 0; // The returned bytesize
// call PSAPI EnumProcesses
if (EnumProcesses(pProcessIds, cb, &dwBytesReturned))
{
// push returned process IDs into the output list
const int nSize = dwBytesReturned / sizeof(DWORD); // Calculate size of array
for(int nIndex = 0; nIndex < nSize; nIndex++) // Loop addresses in array and add to list
{
listProcessIDs.push_back(pProcessIds[nIndex]);
}
}
else // Function call failed, return error
{
dwRet = ::GetLastError();
}
delete[]pProcessIds; // Free memory
return dwRet;
}
/**
* @brief Returns struct holding the information
*/
pInfo* getProcessInformation(DWORD pid)
{
HANDLE procHandle = NULL; // Will hand the Handle to process we're gonna examine
TCHAR filename[MAX_PATH]; // Will hold the filepath to executable file
// Struct will hold returned Information
pInfo* processInformation = new pInfo();
processInformation->path = "";
processInformation->pid = NULL;
processInformation->valid = false;
// Call OpenProcess with given access rights to query information and retrieve a handle to this process
procHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if(procHandle == NULL) // Error opening procHandle
{
#ifdef __USE_DEBUG_MODE__
printf("Failed to open process handle to process %lu \n",pid);
#endif // __USE_DEBUG_MODE__
return processInformation;
}
// Try resolving the filepath and write to filename from handle
if(GetModuleFileNameEx(procHandle, NULL, filename, MAX_PATH) == 0)
{
#ifdef __USE_DEBUG_MODE__
std::cout << "Failed to getModuleFilename" << std::endl;
#endif // __USE_DEBUG_MODE__
}
else
{
#ifdef __USE_DEBUG_MODE__
std::cout << "Module filename is "<<filename<< std::endl;
#endif // __USE_DEBUG_MODE__
// Convert strings
processInformation->path = filename;
processInformation->valid = true;
processInformation->pid = pid;
}
// Close handle to proc.
CloseHandle(procHandle);
// Return processInformation
return processInformation;
}
/**
* @brief Returns List containing pInfos
* @returns 0 is no error
*/
int getPInfosList(std::list<pInfo*> *pInfoList)
{
/// Loop over all PIDs in list
std::list<DWORD> listProcessIDs; // The list to write the PIDs into
const DWORD dwMaxProcessCount = 1024; // The max. amount of processes to loop
// Call the EnumProcesses function to write into list
DWORD dwRet = PSAPI_EnumProcesses(listProcessIDs, dwMaxProcessCount); // Takes list and max. count
// Evaluate response
if(NO_ERROR == dwRet)
{
// No Error returned
const std::list<DWORD>::const_iterator end = listProcessIDs.end(); // Get end iterator
std::list<DWORD>::const_iterator iter = listProcessIDs.begin(); // Get current iterator
// Loop list
for( ; iter != end; ++iter)
{
DWORD dwProcessID = *iter; // Iterator for DWORD list
pInfo* myPInfo = getProcessInformation(dwProcessID); // The Pinfo struct for this Process
// Check if we really want to add it.
// we dont want to add empty strings and duplicates
bool doadd = true;
if(myPInfo->path.size() != 0)
{
for(auto const& i : *pInfoList)
{
if(i->path == myPInfo->path)
{
doadd = false;
}
}
if(doadd)
{
pInfoList->push_back(myPInfo); // Add to pInfo List
}
}
else
{
delete myPInfo;
}
#ifdef __USE_DEBUG_MODE__
std::cout << "Process ID: " << dwProcessID << std::endl;
#endif // __USE_DEBUG_MODE__
}
}
else
{
std::cout << "PSAPI_GetProcessesList failed. Error: " << dwRet << std::endl;
return -99;
}
return 0;
}
/**
* @brief Clears the passed list and destroys objects.
*/
void clearProcList(std::list<pInfo*> *listPInfos)
{
if(!listPInfos->empty())
{
int counter = 0;
for(auto &item: *listPInfos)
{
delete item;
counter ++;
}
listPInfos->clear();
cout << "Cleared " << counter << " Items." << endl;
}
}
#endif // PROCESSFUNCTIONS_H_INCLUDED