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 it's file
50 * i/o so convert to OEM codepage and output
56 /* Convert to OEM, then output */
57 len = WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, NULL, 0, NULL, NULL );
58 mesA = HeapAlloc(GetProcessHeap(), 0, len*sizeof(char));
60 WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, mesA, len, NULL, NULL );
61 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), mesA, len, &count, FALSE);
62 HeapFree(GetProcessHeap(), 0, mesA);
67 Output given message from string table,
69 followed by description of given GetLastError() value to stdout,
70 followed by a trailing newline,
74 static void fatal_error(const WCHAR *msg, DWORD error_code, const WCHAR *filename)
79 static const WCHAR colonsW[] = { ':', ' ', 0 };
80 static const WCHAR newlineW[] = { '\n', 0 };
84 args[0] = (DWORD_PTR)filename;
85 status = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
86 NULL, error_code, 0, (LPWSTR)&lpMsgBuf, 0, (__ms_va_list *)args );
89 WINE_ERR("FormatMessage failed\n");
93 LocalFree((HLOCAL) lpMsgBuf);
99 static void fatal_string_error(int which, DWORD error_code, const WCHAR *filename)
103 if (!LoadStringW(GetModuleHandleW(NULL), which,
104 msg, sizeof(msg)/sizeof(WCHAR)))
105 WINE_ERR("LoadString failed, error %d\n", GetLastError());
107 fatal_error(msg, error_code, filename);
110 static void fatal_string(int which)
114 if (!LoadStringW(GetModuleHandleW(NULL), which,
115 msg, sizeof(msg)/sizeof(WCHAR)))
116 WINE_ERR("LoadString failed, error %d\n", GetLastError());
122 static void usage(void)
124 fatal_string(STRING_USAGE);
127 static void license(void)
129 fatal_string(STRING_LICENSE);
132 static WCHAR *build_args( int argc, WCHAR **argvW )
136 static const WCHAR FormatQuotesW[] = { ' ', '\"', '%', 's', '\"', 0 };
137 static const WCHAR FormatW[] = { ' ', '%', 's', 0 };
139 for (i = 0; i < argc; i++ )
141 wlen += strlenW(argvW[i]) + 1;
142 if (strchrW(argvW[i], ' '))
145 ret = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
148 for (i = 0, p = ret; i < argc; i++ )
150 if (strchrW(argvW[i], ' '))
151 p += sprintfW(p, FormatQuotesW, argvW[i]);
153 p += sprintfW(p, FormatW, argvW[i]);
158 static WCHAR *get_parent_dir(WCHAR* path)
164 last_slash = strrchrW( path, '\\' );
165 if (last_slash == NULL)
168 len = last_slash - path + 1;
170 result = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
171 CopyMemory(result, path, (len-1)*sizeof(WCHAR));
172 result[len-1] = '\0';
177 int wmain (int argc, WCHAR *argv[])
179 SHELLEXECUTEINFOW sei;
184 WCHAR *dos_filename = NULL;
185 WCHAR *parent_directory = NULL;
188 static const WCHAR openW[] = { 'o', 'p', 'e', 'n', 0 };
189 static const WCHAR unixW[] = { 'u', 'n', 'i', 'x', 0 };
190 static const WCHAR progIDOpenW[] =
191 { 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
193 memset(&sei, 0, sizeof(sei));
194 sei.cbSize = sizeof(sei);
196 sei.nShow = SW_SHOWNORMAL;
197 /* Dunno what these mean, but it looks like winMe's start uses them */
198 sei.fMask = SEE_MASK_FLAG_DDEWAIT|
202 /* Canonical Microsoft commandline flag processing:
203 * flags start with /, are case insensitive,
204 * and may be run together in same word.
206 for (i=1; i<argc; i++) {
209 if (argv[i][0] != '/')
212 /* Unix paths can start with / so we have to assume anything following /U is not a flag */
213 if (unix_mode || progid_open)
216 /* Handle all options in this word */
217 for (ci=0; argv[i][ci]; ) {
220 switch(argv[i][ci]) {
223 break; /* FIXME: should stop new window from being created */
226 break; /* FIXME: should ignore any changes to current environment */
230 break; /* notreached */
233 if (argv[i][ci+1] == 'a' || argv[i][ci+1] == 'A')
234 sei.nShow = SW_SHOWMAXIMIZED;
236 sei.nShow = SW_SHOWMINIMIZED;
240 /* sei.nShow = SW_SHOWNORMAL; */
244 if (strncmpiW(&argv[i][ci], unixW, 4) == 0)
247 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
253 if (strncmpiW(&argv[i][ci], progIDOpenW, 17) == 0)
256 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
262 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
268 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
271 /* Skip to next slash */
272 while (argv[i][ci] && (argv[i][ci] != '/'))
281 sei.lpClass = argv[i++];
282 sei.fMask |= SEE_MASK_CLASSNAME;
285 sei.lpFile = argv[i++];
287 args = build_args( argc - i, &argv[i] );
288 sei.lpParameters = args;
290 if (unix_mode || progid_open) {
291 LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR);
292 char* multibyte_unixpath;
293 int multibyte_unixpath_len;
295 wine_get_dos_file_name_ptr = (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
297 if (!wine_get_dos_file_name_ptr)
298 fatal_string(STRING_UNIXFAIL);
300 multibyte_unixpath_len = WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, NULL, 0, NULL, NULL);
301 multibyte_unixpath = HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len);
303 WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, multibyte_unixpath, multibyte_unixpath_len, NULL, NULL);
305 dos_filename = wine_get_dos_file_name_ptr(multibyte_unixpath);
307 HeapFree(GetProcessHeap(), 0, multibyte_unixpath);
310 fatal_string(STRING_UNIXFAIL);
312 sei.lpFile = dos_filename;
313 sei.lpDirectory = parent_directory = get_parent_dir(dos_filename);
314 sei.fMask &= ~SEE_MASK_FLAG_NO_UI;
316 if (GetBinaryTypeW(sei.lpFile, &binary_type)) {
318 STARTUPINFOW startup_info;
319 PROCESS_INFORMATION process_information;
320 static WCHAR commandlineformat[] = {'"','%','s','"','%','s',0};
322 /* 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 */
324 commandline = HeapAlloc(GetProcessHeap(), 0, (strlenW(sei.lpFile)+3+strlenW(sei.lpParameters))*sizeof(WCHAR));
325 sprintfW(commandline, commandlineformat, sei.lpFile, sei.lpParameters);
327 ZeroMemory(&startup_info, sizeof(startup_info));
328 startup_info.cb = sizeof(startup_info);
331 NULL, /* lpApplicationName */
332 commandline, /* lpCommandLine */
333 NULL, /* lpProcessAttributes */
334 NULL, /* lpThreadAttributes */
335 FALSE, /* bInheritHandles */
336 CREATE_NEW_CONSOLE, /* dwCreationFlags */
337 NULL, /* lpEnvironment */
338 sei.lpDirectory, /* lpCurrentDirectory */
339 &startup_info, /* lpStartupInfo */
340 &process_information /* lpProcessInformation */ ))
342 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
344 sei.hProcess = process_information.hProcess;
349 if (!ShellExecuteExW(&sei))
350 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
353 HeapFree( GetProcessHeap(), 0, args );
354 HeapFree( GetProcessHeap(), 0, dos_filename );
355 HeapFree( GetProcessHeap(), 0, parent_directory );
357 if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
359 WaitForSingleObject(sei.hProcess, INFINITE);
360 GetExitCodeProcess(sei.hProcess, &exitcode);
361 ExitProcess(exitcode);