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
6 * Copyright 2007 Lyutin Anatoly (Etersoft)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include <wine/unicode.h>
30 #include <wine/debug.h>
32 #include "resources.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(start);
37 Output given message to stdout without formatting.
39 static void output(const WCHAR *message)
43 int wlen = strlenW(message);
47 res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), message, wlen, &count, NULL);
49 /* If writing to console fails, assume its file
50 i/o so convert to OEM codepage and output */
55 /* Convert to OEM, then output */
56 len = WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, NULL, 0, NULL, NULL );
57 mesA = HeapAlloc(GetProcessHeap(), 0, len*sizeof(char));
59 WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, mesA, len, NULL, NULL );
60 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), mesA, len, &count, FALSE);
61 HeapFree(GetProcessHeap(), 0, mesA);
66 Output given message from string table,
68 followed by description of given GetLastError() value to stdout,
69 followed by a trailing newline,
73 static void fatal_error(const WCHAR *msg, DWORD error_code, const WCHAR *filename)
78 static const WCHAR colonsW[] = { ':', ' ', 0 };
79 static const WCHAR newlineW[] = { '\n', 0 };
83 args[0] = (DWORD_PTR)filename;
84 status = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
85 NULL, error_code, 0, (LPWSTR)&lpMsgBuf, 0, (__ms_va_list *)args );
88 WINE_ERR("FormatMessage failed\n");
92 LocalFree((HLOCAL) lpMsgBuf);
98 static void fatal_string_error(int which, DWORD error_code, const WCHAR *filename)
102 if (!LoadStringW(GetModuleHandleW(NULL), which,
103 msg, sizeof(msg)/sizeof(WCHAR)))
104 WINE_ERR("LoadString failed, error %d\n", GetLastError());
106 fatal_error(msg, error_code, filename);
109 static void fatal_string(int which)
113 if (!LoadStringW(GetModuleHandleW(NULL), which,
114 msg, sizeof(msg)/sizeof(WCHAR)))
115 WINE_ERR("LoadString failed, error %d\n", GetLastError());
121 static void usage(void)
123 fatal_string(STRING_USAGE);
126 static void license(void)
128 fatal_string(STRING_LICENSE);
131 static WCHAR *build_args( int argc, WCHAR **argvW )
135 static const WCHAR FormatQuotesW[] = { ' ', '\"', '%', 's', '\"', 0 };
136 static const WCHAR FormatW[] = { ' ', '%', 's', 0 };
138 for (i = 0; i < argc; i++ )
140 wlen += strlenW(argvW[i]) + 1;
141 if (strchrW(argvW[i], ' '))
144 ret = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
147 for (i = 0, p = ret; i < argc; i++ )
149 if (strchrW(argvW[i], ' '))
150 p += sprintfW(p, FormatQuotesW, argvW[i]);
152 p += sprintfW(p, FormatW, argvW[i]);
157 static WCHAR *get_parent_dir(WCHAR* path)
163 last_slash = strrchrW( path, '\\' );
164 if (last_slash == NULL)
167 len = last_slash - path + 1;
169 result = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
170 CopyMemory(result, path, (len-1)*sizeof(WCHAR));
171 result[len-1] = '\0';
176 int wmain (int argc, WCHAR *argv[])
178 SHELLEXECUTEINFOW sei;
183 WCHAR *dos_filename = NULL;
184 WCHAR *parent_directory = NULL;
187 static const WCHAR openW[] = { 'o', 'p', 'e', 'n', 0 };
188 static const WCHAR unixW[] = { 'u', 'n', 'i', 'x', 0 };
189 static const WCHAR progIDOpenW[] =
190 { 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
192 memset(&sei, 0, sizeof(sei));
193 sei.cbSize = sizeof(sei);
195 sei.nShow = SW_SHOWNORMAL;
196 /* Dunno what these mean, but it looks like winMe's start uses them */
197 sei.fMask = SEE_MASK_FLAG_DDEWAIT|
201 /* Canonical Microsoft commandline flag processing:
202 * flags start with /, are case insensitive,
203 * and may be run together in same word.
205 for (i=1; i<argc; i++) {
208 if (argv[i][0] != '/')
211 /* Unix paths can start with / so we have to assume anything following /U is not a flag */
212 if (unix_mode || progid_open)
215 /* Handle all options in this word */
216 for (ci=0; argv[i][ci]; ) {
219 switch(argv[i][ci]) {
222 break; /* FIXME: should stop new window from being created */
225 break; /* FIXME: should ignore any changes to current environment */
229 break; /* notreached */
232 if (argv[i][ci+1] == 'a' || argv[i][ci+1] == 'A')
233 sei.nShow = SW_SHOWMAXIMIZED;
235 sei.nShow = SW_SHOWMINIMIZED;
239 /* sei.nShow = SW_SHOWNORMAL; */
243 if (strncmpiW(&argv[i][ci], unixW, 4) == 0)
246 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
252 if (strncmpiW(&argv[i][ci], progIDOpenW, 17) == 0)
255 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
261 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
264 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
267 /* Skip to next slash */
268 while (argv[i][ci] && (argv[i][ci] != '/'))
277 sei.lpClass = argv[i++];
278 sei.fMask |= SEE_MASK_CLASSNAME;
281 sei.lpFile = argv[i++];
283 args = build_args( argc - i, &argv[i] );
284 sei.lpParameters = args;
286 if (unix_mode || progid_open) {
287 LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR);
288 char* multibyte_unixpath;
289 int multibyte_unixpath_len;
291 wine_get_dos_file_name_ptr = (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
293 if (!wine_get_dos_file_name_ptr)
294 fatal_string(STRING_UNIXFAIL);
296 multibyte_unixpath_len = WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, NULL, 0, NULL, NULL);
297 multibyte_unixpath = HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len);
299 WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, multibyte_unixpath, multibyte_unixpath_len, NULL, NULL);
301 dos_filename = wine_get_dos_file_name_ptr(multibyte_unixpath);
303 HeapFree(GetProcessHeap(), 0, multibyte_unixpath);
306 fatal_string(STRING_UNIXFAIL);
308 sei.lpFile = dos_filename;
309 sei.lpDirectory = parent_directory = get_parent_dir(dos_filename);
310 sei.fMask &= ~SEE_MASK_FLAG_NO_UI;
312 if (GetBinaryTypeW(sei.lpFile, &binary_type)) {
314 STARTUPINFOW startup_info;
315 PROCESS_INFORMATION process_information;
316 static WCHAR commandlineformat[] = {'"','%','s','"','%','s',0};
318 /* explorer on windows always quotes the filename when running a binary on windows (see bug 5224) so we have to use CreateProcessW in this case */
320 commandline = HeapAlloc(GetProcessHeap(), 0, (strlenW(sei.lpFile)+3+strlenW(sei.lpParameters))*sizeof(WCHAR));
321 sprintfW(commandline, commandlineformat, sei.lpFile, sei.lpParameters);
323 ZeroMemory(&startup_info, sizeof(startup_info));
324 startup_info.cb = sizeof(startup_info);
327 NULL, /* lpApplicationName */
328 commandline, /* lpCommandLine */
329 NULL, /* lpProcessAttributes */
330 NULL, /* lpThreadAttributes */
331 FALSE, /* bInheritHandles */
332 CREATE_NEW_CONSOLE, /* dwCreationFlags */
333 NULL, /* lpEnvironment */
334 sei.lpDirectory, /* lpCurrentDirectory */
335 &startup_info, /* lpStartupInfo */
336 &process_information /* lpProcessInformation */ ))
338 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
340 sei.hProcess = process_information.hProcess;
345 if (!ShellExecuteExW(&sei))
346 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
349 HeapFree( GetProcessHeap(), 0, args );
350 HeapFree( GetProcessHeap(), 0, dos_filename );
351 HeapFree( GetProcessHeap(), 0, parent_directory );
353 if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
355 WaitForSingleObject(sei.hProcess, INFINITE);
356 GetExitCodeProcess(sei.hProcess, &exitcode);
357 ExitProcess(exitcode);