@@ -33,35 +33,24 @@ void print(const char *message, size_t length)
33
33
WriteFile (console, message, size, &size, nullptr );
34
34
}
35
35
36
- DWORD GetProcessByName (PCSTR name)
36
+ static DWORD GetProcessByName (PCSTR name)
37
37
{
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);
42
40
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 );
48
42
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))
51
46
{
52
- do
47
+ if ( wcscmp (process. szExeFile , wide_name) == 0 )
53
48
{
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
+ }
60
51
}
61
52
62
- CloseHandle (snapshot);
63
-
64
- return pid;
53
+ return 0 ;
65
54
}
66
55
67
56
DWORD CALLBACK remote_main (BYTE *image_base)
0 commit comments