-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbgstart.c
More file actions
62 lines (51 loc) · 2.04 KB
/
bgstart.c
File metadata and controls
62 lines (51 loc) · 2.04 KB
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
#include <stdio.h>
#include <string.h>
#include <windows.h>
// Background start for windows
// This starts a process in the background with no
// window. Its useful for console programs that you
// don't wish to see. One difference with this version
// is that it passes on the standard input and output
// handles and waits for the process to end so that
// it can return the exit code.
#ifdef UNICODE
#define tWinMain wWinMain
#else
#define tWinMain WinMain
#endif
int WINAPI tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(nCmdShow);
if (lpCmdLine[0] == TEXT('\0'))
{
MessageBox(NULL,
TEXT("ERROR: No arguments found.\n")
TEXT("This program will launch executables and batch scripts in the background.\n")
TEXT("\nUsage: bgstart [command] <argument 1> <argument 2> ...\n"),
TEXT("BGStart"), MB_ICONERROR);
return 1;
}
STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));
siStartupInfo.cb = sizeof(siStartupInfo);
siStartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
siStartupInfo.wShowWindow = SW_HIDE;
siStartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
siStartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
siStartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
if (CreateProcess(NULL, lpCmdLine, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartupInfo, &piProcessInfo) == FALSE)
{
MessageBox(NULL, TEXT("ERROR: Could not create new process.\n"), TEXT("BGStart"), MB_ICONERROR);
return 1;
}
WaitForSingleObject(piProcessInfo.hProcess, INFINITE);
DWORD ret = 0;
GetExitCodeProcess(piProcessInfo.hProcess, &ret);
CloseHandle(piProcessInfo.hThread);
CloseHandle(piProcessInfo.hProcess);
return ret;
}