Skip to content

Commit 6bd547d

Browse files
perf: use a single snapshot for all functions
1 parent f7450eb commit 6bd547d

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

main.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ thread_local std::string currentParentExe = ""; // to store the name of our own
108108

109109
std::string WideToString(const std::wstring& wstr);
110110

111-
void EnsureCurrentParentExe() {
111+
void EnsureCurrentParentExe(hSnapshot) {
112112
if (!currentParentExe.empty()) return;
113113

114114
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
@@ -311,8 +311,8 @@ std::string GetReadableFileTime(DWORD pid) {
311311
return oss.str();
312312
}
313313

314-
void PrintErrorHints(int errorCode) {
315-
EnsureCurrentParentExe();
314+
void PrintErrorHints(int errorCode, hshot) {
315+
EnsureCurrentParentExe(hshot);
316316
// Use our little lookup table to give hints for specific errors
317317
if (errorHints.find(errorCode) != errorHints.end()) {
318318
if (IsVirtualTerminalModeEnabled()) {
@@ -1490,7 +1490,7 @@ return WideToString(stringBuffer);
14901490
#endif
14911491
}
14921492

1493-
void PrintAncestry(DWORD pid) {
1493+
void PrintAncestry(DWORD pid, hSnapshot) {
14941494
// now we're geting the name
14951495
// we're making it slower by adding a bunch of snapshots
14961496
// but again, we'll optimize and refactor later, i need this to work first
@@ -1512,9 +1512,8 @@ UPDATE: This is done now!!
15121512
PROCESSENTRY32 pe32{};
15131513
pe32.dwSize = sizeof(PROCESSENTRY32);
15141514
DWORD parentPid = 0;
1515-
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
1516-
if (hSnapshot == INVALID_HANDLE_VALUE) return;
15171515

1516+
15181517

15191518
DWORD currentProcessId = GetCurrentProcessId(); // checking our own process
15201519
DWORD currentParentPid = 0;
@@ -1727,7 +1726,7 @@ void FindProcessPorts(DWORD targetPid) {
17271726

17281727

17291728

1730-
void PIDinspect(const std::vector<DWORD>& pids, const std::vector<std::string>& names) { // ooh guys look i'm in the void
1729+
void PIDinspect(const std::vector<DWORD>& pids, const std::vector<std::string>& names, hshot) { // ooh guys look i'm in the void
17311730
DWORD pid = pids[0];
17321731
std::string procName = GetProcessNameFromPid(pid);
17331732
if (IsVirtualTerminalModeEnabled()) {
@@ -1782,7 +1781,7 @@ void PIDinspect(const std::vector<DWORD>& pids, const std::vector<std::string>&
17821781

17831782
}
17841783
if (queryError) {
1785-
PrintErrorHints(errorCode);
1784+
PrintErrorHints(errorCode, hshot);
17861785
}
17871786

17881787

@@ -1813,7 +1812,7 @@ void PIDinspect(const std::vector<DWORD>& pids, const std::vector<std::string>&
18131812
<< "\n Maybe Access is Denied or the process is running entirely in RAM." << std::endl;
18141813
}
18151814
if (queryError) {
1816-
PrintErrorHints(errorCode);
1815+
PrintErrorHints(errorCode, hshot);
18171816
// it might seem like overkill to call the function every time there's an error,
18181817
// but if you remember we have a fallback for opening processes, so there are multiple
18191818
// places where an error can occur.
@@ -1925,7 +1924,7 @@ std::string FRAM = ""; // fram means formatted ram, i'm so creative at var namin
19251924
} else {
19261925
std::cout << "\nWhy It Exists:\n";
19271926
}
1928-
PrintAncestry(pid);
1927+
PrintAncestry(pid, hshot);
19291928

19301929
FindProcessPorts(pid);
19311930

@@ -1989,17 +1988,15 @@ struct ProcInfos {
19891988
std::vector<int> pids;
19901989
};
19911990

1992-
ProcInfos findMyProc(const char *procname) {
1991+
ProcInfos findMyProc(const char *procname, hSnapshot) {
19931992

1994-
HANDLE hSnapshot;
1993+
19951994
PROCESSENTRY32 pe;
19961995
ProcInfos result;
19971996
BOOL hResult;
19981997

19991998

2000-
// snapshot of all processes in the system
2001-
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2002-
if (INVALID_HANDLE_VALUE == hSnapshot) return {};
1999+
20032000

20042001
// initializing size: needed for using Process32First
20052002
pe.dwSize = sizeof(PROCESSENTRY32);
@@ -2142,8 +2139,11 @@ int main(int argc, char* argv[]) {
21422139
std::vector<std::string> trash;
21432140
trash.push_back("");
21442141
pids.push_back(static_cast<DWORD>(pid));// function requires it to be a list even if only 1 is passed
2145-
2146-
PIDinspect(pids, trash);
2142+
// snapshot of all processes in the system first so we can pass it to every function from there on
2143+
2144+
HANDLE hshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2145+
if (INVALID_HANDLE_VALUE == hSnapshot) return {};
2146+
PIDinspect(pids, trash, hshot);
21472147
} else {
21482148
if (IsVirtualTerminalModeEnabled()) { // ugh i have to do this EVERY SINGLE TIME
21492149
std::cerr << "\033[1;31mError:\033[0m --pid option requires an argument." << std::endl;
@@ -2161,10 +2161,12 @@ int main(int argc, char* argv[]) {
21612161
// check for process name if no recognized flags
21622162
else if (arg[0] != '-') { // if it doesn't start with -- or -
21632163
std::string procName = arg;
2164-
ProcInfos r = findMyProc(procName.c_str());
2164+
HANDLE hshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2165+
if (INVALID_HANDLE_VALUE == hSnapshot) return {};
2166+
ProcInfos r = findMyProc(procName.c_str(), hshot);
21652167
if (!r.pids.empty()) {
21662168
std::vector<DWORD> dwPids(r.pids.begin(), r.pids.end());
2167-
PIDinspect(dwPids, r.names);
2169+
PIDinspect(dwPids, r.names, hshot);
21682170
} else {
21692171
if (IsVirtualTerminalModeEnabled()) {
21702172
std::cerr << "\033[1;31mError:\033[0m Could not find process with name " << procName << "." << std::endl;

0 commit comments

Comments
 (0)