46 |
static HINSTANCE g_instance; |
static HINSTANCE g_instance; |
47 |
static HWND g_hwnd; |
static HWND g_hwnd; |
48 |
|
|
49 |
|
static DWORD g_session_id; |
50 |
|
static DWORD *g_startup_procs; |
51 |
|
static int g_startup_num_procs; |
52 |
|
|
53 |
typedef void (*set_hooks_proc_t) (); |
typedef void (*set_hooks_proc_t) (); |
54 |
typedef void (*remove_hooks_proc_t) (); |
typedef void (*remove_hooks_proc_t) (); |
55 |
typedef int (*get_instance_count_proc_t) (); |
typedef int (*get_instance_count_proc_t) (); |
279 |
return TRUE; |
return TRUE; |
280 |
} |
} |
281 |
|
|
282 |
|
static BOOL |
283 |
|
build_startup_procs(void) |
284 |
|
{ |
285 |
|
PWTS_PROCESS_INFO pinfo; |
286 |
|
DWORD i, j, count; |
287 |
|
|
288 |
|
if (!WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pinfo, &count)) |
289 |
|
return FALSE; |
290 |
|
|
291 |
|
g_startup_num_procs = 0; |
292 |
|
|
293 |
|
for (i = 0; i < count; i++) |
294 |
|
{ |
295 |
|
if (pinfo[i].SessionId != g_session_id) |
296 |
|
continue; |
297 |
|
|
298 |
|
g_startup_num_procs++; |
299 |
|
} |
300 |
|
|
301 |
|
g_startup_procs = malloc(sizeof(DWORD) * g_startup_num_procs); |
302 |
|
|
303 |
|
j = 0; |
304 |
|
for (i = 0; i < count; i++) |
305 |
|
{ |
306 |
|
if (pinfo[i].SessionId != g_session_id) |
307 |
|
continue; |
308 |
|
|
309 |
|
g_startup_procs[j] = pinfo[i].ProcessId; |
310 |
|
j++; |
311 |
|
} |
312 |
|
|
313 |
|
WTSFreeMemory(pinfo); |
314 |
|
|
315 |
|
return TRUE; |
316 |
|
} |
317 |
|
|
318 |
|
static void |
319 |
|
free_startup_procs(void) |
320 |
|
{ |
321 |
|
free(g_startup_procs); |
322 |
|
|
323 |
|
g_startup_procs = NULL; |
324 |
|
g_startup_num_procs = 0; |
325 |
|
} |
326 |
|
|
327 |
|
static BOOL |
328 |
|
should_terminate(void) |
329 |
|
{ |
330 |
|
PWTS_PROCESS_INFO pinfo; |
331 |
|
DWORD i, j, count; |
332 |
|
|
333 |
|
if (!WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pinfo, &count)) |
334 |
|
return TRUE; |
335 |
|
|
336 |
|
for (i = 0; i < count; i++) |
337 |
|
{ |
338 |
|
if (pinfo[i].SessionId != g_session_id) |
339 |
|
continue; |
340 |
|
|
341 |
|
for (j = 0; j < g_startup_num_procs; j++) |
342 |
|
{ |
343 |
|
if (pinfo[i].ProcessId == g_startup_procs[j]) |
344 |
|
break; |
345 |
|
} |
346 |
|
|
347 |
|
if (j == g_startup_num_procs) |
348 |
|
{ |
349 |
|
WTSFreeMemory(pinfo); |
350 |
|
return FALSE; |
351 |
|
} |
352 |
|
} |
353 |
|
|
354 |
|
WTSFreeMemory(pinfo); |
355 |
|
|
356 |
|
return TRUE; |
357 |
|
} |
358 |
|
|
359 |
int WINAPI |
int WINAPI |
360 |
WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow) |
WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow) |
361 |
{ |
{ |
407 |
|
|
408 |
WTSRegisterSessionNotification(g_hwnd, NOTIFY_FOR_THIS_SESSION); |
WTSRegisterSessionNotification(g_hwnd, NOTIFY_FOR_THIS_SESSION); |
409 |
|
|
410 |
|
ProcessIdToSessionId(GetCurrentProcessId(), &g_session_id); |
411 |
|
|
412 |
|
build_startup_procs(); |
413 |
|
|
414 |
vchannel_open(); |
vchannel_open(); |
415 |
|
|
416 |
vchannel_write("HELLO", "0x%08x", 0); |
vchannel_write("HELLO", "0x%08x", 0); |
435 |
else |
else |
436 |
{ |
{ |
437 |
BOOL result; |
BOOL result; |
|
DWORD exitcode; |
|
438 |
PROCESS_INFORMATION proc_info; |
PROCESS_INFORMATION proc_info; |
439 |
STARTUPINFO startup_info; |
STARTUPINFO startup_info; |
440 |
MSG msg; |
MSG msg; |
444 |
|
|
445 |
result = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, |
result = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, |
446 |
NULL, NULL, &startup_info, &proc_info); |
NULL, NULL, &startup_info, &proc_info); |
447 |
|
// Release handles |
448 |
|
CloseHandle(proc_info.hProcess); |
449 |
|
CloseHandle(proc_info.hThread); |
450 |
|
|
451 |
if (result) |
if (result) |
452 |
{ |
{ |
453 |
do |
int check_counter; |
454 |
|
|
455 |
|
check_counter = 5; |
456 |
|
while (check_counter-- || !should_terminate()) |
457 |
{ |
{ |
458 |
|
if (check_counter < 0) |
459 |
|
check_counter = 5; |
460 |
|
|
461 |
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) |
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) |
462 |
{ |
{ |
463 |
TranslateMessage(&msg); |
TranslateMessage(&msg); |
465 |
} |
} |
466 |
process_cmds(); |
process_cmds(); |
467 |
Sleep(100); |
Sleep(100); |
|
GetExitCodeProcess(proc_info.hProcess, &exitcode); |
|
468 |
} |
} |
|
while (exitcode == STILL_ACTIVE); |
|
|
|
|
|
// Release handles |
|
|
CloseHandle(proc_info.hProcess); |
|
|
CloseHandle(proc_info.hThread); |
|
469 |
} |
} |
470 |
else |
else |
471 |
{ |
{ |
485 |
|
|
486 |
vchannel_close(); |
vchannel_close(); |
487 |
|
|
488 |
|
free_startup_procs(); |
489 |
|
|
490 |
return 1; |
return 1; |
491 |
} |
} |