This repository was archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathtask.c
72 lines (57 loc) · 1.43 KB
/
task.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
/**
* Run the passed command line hidden, suitable for a scheduled task.
* Author: Anatol Belski <ab@php.net>
* License: BSD 2-Clause
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define CMD_PRE "cmd.exe /c "
#define CMD_PRE_LEN (sizeof(CMD_PRE)-1)
#ifdef DEBUG
int
main(int argc, char **argv)
#else
int
APIENTRY WinMain(HINSTANCE inst, HINSTANCE prev_inst, LPTSTR in_cmd, int show)
#endif
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD exit_code;
char *cmd = NULL;
size_t cmd_len = 0, arg_len = 0;
char *arg = strchr(GetCommandLine(), ' ');
if (!arg) {
return 3;
}
#ifdef DEBUG
printf("passed cmd: '%s'\n", arg);
#endif
arg_len = strlen(arg);
cmd_len = CMD_PRE_LEN + arg_len + 1;
cmd = malloc(cmd_len * sizeof(char));
memmove(cmd, CMD_PRE, CMD_PRE_LEN);
memmove(cmd + CMD_PRE_LEN, arg, arg_len);
cmd[cmd_len-1] = '\0';
#ifdef DEBUG
printf("constructed cmd: '%s'\n", cmd);
#endif
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
ZeroMemory( &pi, sizeof(pi) );
if (CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
CloseHandle( pi.hThread );
} else {
free(cmd);
printf( "Error: CreatePracess 0x%08lx \n", GetLastError() );
return 3;
}
WaitForSingleObject( pi.hProcess, INFINITE );
GetExitCodeProcess(pi.hProcess, &exit_code);
CloseHandle( pi.hProcess );
free(cmd);
return exit_code;
}