/
TProcessCtrl_FindProcess.c
242 lines (227 loc) · 5.57 KB
/
TProcessCtrl_FindProcess.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#define _CRT_NONSTDC_NO_WARNINGS
#ifdef __BORLANDC__
#define string_empty(s) (s)->empty()
#define string_length(s) (s)->length()
#define string_begin(s) (s)->begin()
#define string_end(s) (s)->end()
#define string_assign_cstr(s, cstr) *(s) = (cstr);
#else
#include <windows.h>
#define USING_NAMESPACE_BCB6_STD
#include "bcb6_std_string.h"
#endif
#include <mbstring.h>
#include "intrinsic.h"
#include "tlhelp32fix.h"
#include "ProcessMonitor.h"
#include "FindWindowContainsModule.h"
#include "GetFileTitlePointer.h"
extern HANDLE hHeap;
static char * __fastcall FindInvalidChar(const char *string)
{
while (*string)
{
if (!__intrinsic_isleadbyte(*string))
{
switch (*string)
{
case '\\':
case '/':
case ':':
case '*':
case '?':
case '"':
case '<':
case '>':
case '|':
return (char *)string;
}
}
else
{
if (!*(++string))
break;
}
string++;
}
return NULL;
}
static char ** __stdcall ParseArgument(const char *begin, const char *end, size_t *argc)
{
*argc = 0;
while (*begin == ' ')
begin++;
if (end > begin)
{
size_t length, maxArgCount, capacity;
char **argv;
length = end - begin;
maxArgCount = (length + 1) / 2;
capacity = maxArgCount * sizeof(char *) + (length + 1) * sizeof(char);
argv = (char **)HeapAlloc(hHeap, 0, capacity);
if (argv)
{
char *p;
BOOLEAN inDoubleQuote;
size_t index;
p = (char *)(argv + maxArgCount);
*(char *)(end = p + length) = '\0';
memcpy(p, begin, length * sizeof(char));
inDoubleQuote = FALSE;
argv[index = 0] = p;
while (*p)
{
if (!__intrinsic_isleadbyte(*p))
{
switch (*p)
{
case ' ':
if (inDoubleQuote)
break;
*p = '\0';
while (*(++p) == ' ');
if (*p)
argv[++index] = p;
continue;
case '"':
inDoubleQuote = !inDoubleQuote;
memcpy(p, p + 1, ((end--) - p) * sizeof(char));
continue;
case '\\':
length = 0;
while (p[++length] == '\\');
if (p[length] == '"')
{
const char *src;
length = (length + 1) / 2;
src = p + length;
memcpy(p, src, (end - src + 1) * sizeof(char));
end -= length;
}
p += length;
continue;
}
}
else if (!*(++p))
break;
p++;
}
*argc = index + 1;
return argv;
}
}
return NULL;
}
#ifdef __BORLANDC__
unsigned long TProcessCtrl::FindProcess(string *ProcessName, PROCESSENTRY32 *Entry)
#else
unsigned long __cdecl TProcessCtrl_FindProcess(LPVOID this, string *ProcessName, PROCESSENTRY32A *Entry)
#endif
{
#define CLASSNAME_BRACKET_OPEN '<'
#define CLASSNAME_BRACKET_CLOSE '>'
#define MODULENAME_PREFIX ':'
#define WINDOWNAME_PREFIX '*'
#define OPTION_PREFIX '/'
do /* do { ... } while (0); */
{
DWORD dwProcessId;
HANDLE hSnapshot;
PROCESSENTRY32A pe;
if (string_empty(ProcessName))
break;
if (!FindInvalidChar(string_begin(ProcessName)))
{
dwProcessId = FindProcessId(FALSE, string_begin(ProcessName), string_length(ProcessName), NULL);
}
else
{
char **argv;
size_t argc;
argv = ParseArgument(string_begin(ProcessName), string_end(ProcessName), &argc);
if (!argv)
break;
dwProcessId = 0;
do /* do { ... } while (0); */
{
BOOL bIsRegex;
LPSTR lpProcessName;
LPSTR lpClassName;
LPSTR lpWindowName;
LPSTR lpModuleName;
size_t length;
bIsRegex = FALSE;
lpProcessName = NULL;
lpClassName = NULL;
lpWindowName = NULL;
lpModuleName = NULL;
for (size_t i = 0; i < argc; i++)
{
switch (*argv[i])
{
case CLASSNAME_BRACKET_OPEN:
if (lpClassName)
break;
length = strlen(argv[i] + 1);
if (!length || argv[i][length] != CLASSNAME_BRACKET_CLOSE)
break;
argv[i][length] = '\0';
lpClassName = argv[i] + 1;
break;
case WINDOWNAME_PREFIX:
if (!lpWindowName)
lpWindowName = argv[i] + 1;
break;
case MODULENAME_PREFIX:
if (!lpModuleName)
lpModuleName = argv[i] + 1;
break;
case OPTION_PREFIX:
if (stricmp(argv[i] + 1, "regex") == 0)
bIsRegex = TRUE;
break;
default:
if (!lpProcessName)
lpProcessName = argv[i];
break;
}
}
if (lpProcessName || (lpModuleName && !lpClassName && !lpWindowName))
{
dwProcessId = FindProcessId(bIsRegex, lpProcessName, lpProcessName ? strlen(lpProcessName) : 0, lpModuleName);
}
else if (lpClassName || lpWindowName)
{
if (FindWindowContainsModule(bIsRegex, lpClassName, lpWindowName, lpModuleName, &dwProcessId))
StopProcessMonitor();
}
} while (0);
HeapFree(hHeap, 0, argv);
}
if (!dwProcessId)
break;
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
break;
pe.dwSize = sizeof(PROCESSENTRY32A);
if (Process32FirstA(hSnapshot, &pe))
{
do
{
if (pe.th32ProcessID != dwProcessId)
continue;
CloseHandle(hSnapshot);
string_assign_cstr(ProcessName, GetFileTitlePointerA(pe.szExeFile));
*Entry = pe;
return 0;
} while (Process32NextA(hSnapshot, &pe));
}
CloseHandle(hSnapshot);
} while (0);
return 1;
#undef CLASSNAME_BRACKET_OPEN
#undef CLASSNAME_BRACKET_CLOSE
#undef MODULENAME_PREFIX
#undef WINDOWNAME_PREFIX
#undef OPTION_PREFIX
}