Skip to content

Commit 33f1e65

Browse files
committed
Simplify process enumeration loop
1 parent d18e862 commit 33f1e65

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

source/main.cpp

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,24 @@ void print(const char *message, size_t length)
3333
WriteFile(console, message, size, &size, nullptr);
3434
}
3535

36-
DWORD GetProcessByName(PCSTR name)
36+
static DWORD GetProcessByName(PCSTR name)
3737
{
38-
DWORD pid = 0;
39-
40-
WCHAR exe[MAX_PATH] = {};
41-
mbstowcs_s(NULL, exe, name, MAX_PATH);
38+
WCHAR wide_name[MAX_PATH] = {};
39+
mbstowcs_s(NULL, wide_name, name, MAX_PATH);
4240

43-
// Create toolhelp snapshot.
44-
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
45-
PROCESSENTRY32 process;
46-
ZeroMemory(&process, sizeof(process));
47-
process.dwSize = sizeof(process);
41+
const scoped_handle snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4842

49-
// Walkthrough all processes.
50-
if (Process32First(snapshot, &process))
43+
// Walk through all processes and search for the name
44+
PROCESSENTRY32W process = { sizeof(process) };
45+
for (BOOL next = Process32FirstW(snapshot, &process); next; next = Process32NextW(snapshot, &process))
5146
{
52-
do
47+
if (wcscmp(process.szExeFile, wide_name) == 0)
5348
{
54-
if (wcscmp(process.szExeFile, exe) == 0)
55-
{
56-
pid = process.th32ProcessID;
57-
break;
58-
}
59-
} while (Process32Next(snapshot, &process));
49+
return process.th32ProcessID;
50+
}
6051
}
6152

62-
CloseHandle(snapshot);
63-
64-
return pid;
53+
return 0;
6554
}
6655

6756
DWORD CALLBACK remote_main(BYTE *image_base)

0 commit comments

Comments
 (0)