|
1 | | -/** |
2 | | - * Run the passed command line hidden, suitable for a scheduled task. |
3 | | - * Author: Anatol Belski <ab@php.net> |
4 | | - * License: BSD 2-Clause |
5 | | - */ |
6 | | - |
7 | | -#include <windows.h> |
8 | | -#include <stdio.h> |
9 | | -#include <stdlib.h> |
10 | | - |
11 | | -#define CMD_PRE "cmd.exe /c " |
12 | | -#define CMD_PRE_LEN (sizeof(CMD_PRE)-1) |
13 | | - |
14 | | -#ifdef DEBUG |
15 | | -int |
16 | | -main(int argc, char **argv) |
17 | | -#else |
18 | | -int |
19 | | -APIENTRY WinMain(HINSTANCE inst, HINSTANCE prev_inst, LPTSTR in_cmd, int show) |
20 | | -#endif |
21 | | -{ |
22 | | - STARTUPINFO si; |
23 | | - PROCESS_INFORMATION pi; |
24 | | - DWORD exit_code; |
25 | | - |
26 | | - char *cmd = NULL; |
27 | | - size_t cmd_len = 0, arg_len = 0; |
28 | | - char *arg = strchr(GetCommandLine(), ' '); |
29 | | - |
30 | | - if (!arg) { |
31 | | - return 3; |
32 | | - } |
33 | | -#ifdef DEBUG |
34 | | - printf("passed cmd: '%s'\n", arg); |
35 | | -#endif |
36 | | - |
37 | | - arg_len = strlen(arg); |
38 | | - cmd_len = CMD_PRE_LEN + arg_len + 1; |
39 | | - |
40 | | - cmd = malloc(cmd_len * sizeof(char)); |
41 | | - memmove(cmd, CMD_PRE, CMD_PRE_LEN); |
42 | | - memmove(cmd + CMD_PRE_LEN, arg, arg_len); |
43 | | - cmd[cmd_len-1] = '\0'; |
44 | | -#ifdef DEBUG |
45 | | - printf("constructed cmd: '%s'\n", cmd); |
46 | | -#endif |
47 | | - |
48 | | - ZeroMemory( &si, sizeof(si) ); |
49 | | - si.cb = sizeof(si); |
50 | | - si.dwFlags = STARTF_USESHOWWINDOW; |
51 | | - si.wShowWindow = SW_HIDE; |
52 | | - ZeroMemory( &pi, sizeof(pi) ); |
53 | | - |
54 | | - if (CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) { |
55 | | - CloseHandle( pi.hThread ); |
56 | | - } else { |
57 | | - free(cmd); |
58 | | - printf( "Error: CreatePracess 0x%08lx \n", GetLastError() ); |
59 | | - return 3; |
60 | | - } |
61 | | - |
62 | | - WaitForSingleObject( pi.hProcess, INFINITE ); |
63 | | - |
64 | | - GetExitCodeProcess(pi.hProcess, &exit_code); |
65 | | - |
66 | | - CloseHandle( pi.hProcess ); |
67 | | - |
68 | | - free(cmd); |
69 | | - |
70 | | - return exit_code; |
71 | | -} |
72 | | -
|
| 1 | +/** |
| 2 | + * Run the passed command line hidden, suitable for a scheduled task. |
| 3 | + * Author: Anatol Belski <ab@php.net> |
| 4 | + * License: BSD 2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <windows.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | + |
| 11 | +#define CMD_PRE "cmd.exe /c " |
| 12 | +#define CMD_PRE_LEN (sizeof(CMD_PRE)-1) |
| 13 | + |
| 14 | +#ifdef DEBUG |
| 15 | +int |
| 16 | +main(int argc, char **argv) |
| 17 | +#else |
| 18 | +int |
| 19 | +APIENTRY WinMain(HINSTANCE inst, HINSTANCE prev_inst, LPTSTR in_cmd, int show) |
| 20 | +#endif |
| 21 | +{ |
| 22 | + STARTUPINFO si; |
| 23 | + PROCESS_INFORMATION pi; |
| 24 | + DWORD exit_code; |
| 25 | + |
| 26 | + char *cmd = NULL; |
| 27 | + size_t cmd_len = 0, arg_len = 0; |
| 28 | + char *arg = strchr(GetCommandLine(), ' '); |
| 29 | + |
| 30 | + if (!arg) { |
| 31 | + return 3; |
| 32 | + } |
| 33 | +#ifdef DEBUG |
| 34 | + printf("passed cmd: '%s'\n", arg); |
| 35 | +#endif |
| 36 | + |
| 37 | + arg_len = strlen(arg); |
| 38 | + cmd_len = CMD_PRE_LEN + arg_len + 1; |
| 39 | + |
| 40 | + cmd = malloc(cmd_len * sizeof(char)); |
| 41 | + memmove(cmd, CMD_PRE, CMD_PRE_LEN); |
| 42 | + memmove(cmd + CMD_PRE_LEN, arg, arg_len); |
| 43 | + cmd[cmd_len-1] = '\0'; |
| 44 | +#ifdef DEBUG |
| 45 | + printf("constructed cmd: '%s'\n", cmd); |
| 46 | +#endif |
| 47 | + |
| 48 | + ZeroMemory( &si, sizeof(si) ); |
| 49 | + si.cb = sizeof(si); |
| 50 | + si.dwFlags = STARTF_USESHOWWINDOW; |
| 51 | + si.wShowWindow = SW_HIDE; |
| 52 | + ZeroMemory( &pi, sizeof(pi) ); |
| 53 | + |
| 54 | + if (CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) { |
| 55 | + CloseHandle( pi.hThread ); |
| 56 | + } else { |
| 57 | + free(cmd); |
| 58 | + printf( "Error: CreatePracess 0x%08lx \n", GetLastError() ); |
| 59 | + return 3; |
| 60 | + } |
| 61 | + |
| 62 | + WaitForSingleObject( pi.hProcess, INFINITE ); |
| 63 | + |
| 64 | + GetExitCodeProcess(pi.hProcess, &exit_code); |
| 65 | + |
| 66 | + CloseHandle( pi.hProcess ); |
| 67 | + |
| 68 | + free(cmd); |
| 69 | + |
| 70 | + return exit_code; |
| 71 | +} |
| 72 | + |
0 commit comments