2 * Start a program using ShellExecuteEx, optionally wait for it to finish
3 * Compatible with Microsoft's "c:\windows\command\start.exe"
5 * Copyright 2003 Dan Kegel
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "resources.h"
31 Output given message to stdout without formatting.
33 static void output(const char *message)
36 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), message, strlen(message), &count, NULL);
42 followed by description of given GetLastError() value to stdout,
43 followed by a trailing newline,
46 static void fatal_error(const char *msg, DWORD error_code)
53 status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, (LPTSTR) & lpMsgBuf, 0, NULL);
55 output("FormatMessage failed\n");
58 LocalFree((HLOCAL) lpMsgBuf);
65 Output given message from string table,
67 followed by description of given GetLastError() value to stdout,
68 followed by a trailing newline,
71 static void fatal_string_error(int which, DWORD error_code)
75 if (!LoadString(GetModuleHandle(NULL), which,
77 fatal_error("LoadString failed", GetLastError());
79 fatal_error(msg, error_code);
82 static void fatal_string(int which)
86 if (!LoadString(GetModuleHandle(NULL), which,
88 fatal_error("LoadString failed", GetLastError());
94 static void usage(void)
96 fatal_string(STRING_USAGE);
99 static void license(void)
101 fatal_string(STRING_LICENSE);
104 static char *build_args( int argc, char **argv )
109 for (i = 0; i < argc; i++ )
110 len += strlen(argv[i]) + 1;
111 ret = HeapAlloc( GetProcessHeap(), 0, len );
114 for (i = 0, p = ret; i < argc; i++ )
115 p += sprintf(p, " %s", argv[i]);
119 int main(int argc, char *argv[])
121 SHELLEXECUTEINFO sei;
125 memset(&sei, 0, sizeof(sei));
126 sei.cbSize = sizeof(sei);
128 sei.nShow = SW_SHOWNORMAL;
129 /* Dunno what these mean, but it looks like winMe's start uses them */
130 sei.fMask = SEE_MASK_FLAG_DDEWAIT|SEE_MASK_FLAG_NO_UI;
132 /* Canonical Microsoft commandline flag processing:
133 * flags start with /, are case insensitive,
134 * and may be run together in same word.
136 for (i=1; i<argc; i++) {
139 if (argv[i][0] != '/')
142 /* Handle all options in this word */
143 for (ci=0; argv[i][ci]; ) {
146 switch(argv[i][ci]) {
150 break; /* notreached */
153 if (argv[i][ci+1] == 'a' || argv[i][ci+1] == 'A')
154 sei.nShow = SW_SHOWMAXIMIZED;
156 sei.nShow = SW_SHOWMINIMIZED;
160 /* sei.nShow = SW_SHOWNORMAL; */
164 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
167 printf("Option '%s' not recognized\n", argv[i]+ci-1);
170 /* Skip to next slash */
171 while (argv[i][ci] && (argv[i][ci] != '/'))
179 sei.lpFile = argv[i++];
181 args = build_args( argc - i, &argv[i] );
182 sei.lpParameters = args;
184 if (!ShellExecuteEx(&sei))
185 fatal_string_error(STRING_EXECFAIL, GetLastError());
187 HeapFree( GetProcessHeap(), 0, args );
189 if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
192 waitcode = WaitForSingleObject(sei.hProcess, INFINITE);
194 fatal_error("WaitForSingleObject", GetLastError());
195 if (!GetExitCodeProcess(sei.hProcess, &exitcode))
196 fatal_error("GetExitCodeProcess", GetLastError());
197 /* fixme: haven't tested whether exit code works properly */
198 ExitProcess(exitcode);