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/debug.h>
31 #include "resources.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(start
);
36 Output given message to stdout without formatting.
38 static void output(const WCHAR
*message
)
42 int wlen
= lstrlenW(message
);
46 res
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), message
, wlen
, &count
, NULL
);
48 /* If writing to console fails, assume it's file
49 * 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
, msg
, ARRAY_SIZE(msg
)))
103 WINE_ERR("LoadString failed, error %d\n", GetLastError());
105 fatal_error(msg
, error_code
, filename
);
108 static void fatal_string(int which
)
112 if (!LoadStringW(GetModuleHandleW(NULL
), which
, msg
, ARRAY_SIZE(msg
)))
113 WINE_ERR("LoadString failed, error %d\n", GetLastError());
119 static void usage(void)
121 fatal_string(STRING_USAGE
);
124 static WCHAR
*build_args( int argc
, WCHAR
**argvW
)
128 static const WCHAR FormatQuotesW
[] = { ' ', '\"', '%', 's', '\"', 0 };
129 static const WCHAR FormatW
[] = { ' ', '%', 's', 0 };
131 for (i
= 0; i
< argc
; i
++ )
133 wlen
+= lstrlenW(argvW
[i
]) + 1;
134 if (wcschr(argvW
[i
], ' '))
137 ret
= HeapAlloc( GetProcessHeap(), 0, wlen
*sizeof(WCHAR
) );
140 for (i
= 0, p
= ret
; i
< argc
; i
++ )
142 if (wcschr(argvW
[i
], ' '))
143 p
+= swprintf(p
, wlen
- (p
- ret
), FormatQuotesW
, argvW
[i
]);
145 p
+= swprintf(p
, wlen
- (p
- ret
), FormatW
, argvW
[i
]);
150 static WCHAR
*get_parent_dir(WCHAR
* path
)
156 last_slash
= wcsrchr( path
, '\\' );
157 if (last_slash
== NULL
)
160 len
= last_slash
- path
+ 1;
162 result
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
163 CopyMemory(result
, path
, (len
-1)*sizeof(WCHAR
));
164 result
[len
-1] = '\0';
169 static BOOL
is_option(const WCHAR
* arg
, const WCHAR
* opt
)
171 return CompareStringW(LOCALE_USER_DEFAULT
, NORM_IGNORECASE
,
172 arg
, -1, opt
, -1) == CSTR_EQUAL
;
175 int wmain (int argc
, WCHAR
*argv
[])
177 SHELLEXECUTEINFOW sei
;
178 DWORD creation_flags
;
181 BOOL unix_mode
= FALSE
;
182 BOOL progid_open
= FALSE
;
184 WCHAR
*dos_filename
= NULL
;
185 WCHAR
*parent_directory
= NULL
;
188 static const WCHAR bW
[] = { '/', 'b', 0 };
189 static const WCHAR minW
[] = { '/', 'm', 'i', 'n', 0 };
190 static const WCHAR maxW
[] = { '/', 'm', 'a', 'x', 0 };
191 static const WCHAR lowW
[] = { '/', 'l', 'o', 'w', 0 };
192 static const WCHAR normalW
[] = { '/', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
193 static const WCHAR highW
[] = { '/', 'h', 'i', 'g', 'h', 0 };
194 static const WCHAR realtimeW
[] = { '/', 'r', 'e', 'a', 'l', 't', 'i', 'm', 'e', 0 };
195 static const WCHAR abovenormalW
[] = { '/', 'a', 'b', 'o', 'v', 'e', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
196 static const WCHAR belownormalW
[] = { '/', 'b', 'e', 'l', 'o', 'w', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
197 static const WCHAR separateW
[] = { '/', 's', 'e', 'p', 'a', 'r', 'a', 't', 'e', 0 };
198 static const WCHAR sharedW
[] = { '/', 's', 'h', 'a', 'r', 'e', 'd', 0 };
199 static const WCHAR nodeW
[] = { '/', 'n', 'o', 'd', 'e', 0 };
200 static const WCHAR affinityW
[] = { '/', 'a', 'f', 'f', 'i', 'n', 'i', 't', 'y', 0 };
201 static const WCHAR wW
[] = { '/', 'w', 0 };
202 static const WCHAR waitW
[] = { '/', 'w', 'a', 'i', 't', 0 };
203 static const WCHAR helpW
[] = { '/', '?', 0 };
204 static const WCHAR unixW
[] = { '/', 'u', 'n', 'i', 'x', 0 };
205 static const WCHAR progIDOpenW
[] =
206 { '/', 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
207 static const WCHAR openW
[] = { 'o', 'p', 'e', 'n', 0 };
208 static const WCHAR cmdW
[] = { 'c', 'm', 'd', '.', 'e', 'x', 'e', 0 };
210 memset(&sei
, 0, sizeof(sei
));
211 sei
.cbSize
= sizeof(sei
);
213 sei
.nShow
= SW_SHOWNORMAL
;
214 /* Dunno what these mean, but it looks like winMe's start uses them */
215 sei
.fMask
= SEE_MASK_FLAG_DDEWAIT
|
217 sei
.lpDirectory
= NULL
;
218 creation_flags
= CREATE_NEW_CONSOLE
;
220 /* Canonical Microsoft commandline flag processing:
221 * flags start with / and are case insensitive.
223 for (i
=1; i
<argc
; i
++) {
224 /* parse first quoted argument as console title */
225 if (!title
&& argv
[i
][0] == '"') {
229 if (argv
[i
][0] != '/')
232 /* Unix paths can start with / so we have to assume anything following /unix is not a flag */
233 if (unix_mode
|| progid_open
)
236 if (argv
[i
][0] == '/' && (argv
[i
][1] == 'd' || argv
[i
][1] == 'D')) {
238 /* The start directory was concatenated to the option */
239 sei
.lpDirectory
= argv
[i
]+2;
240 else if (i
+1 == argc
) {
241 WINE_ERR("you must specify a directory path for the /d option\n");
244 sei
.lpDirectory
= argv
[++i
];
246 else if (is_option(argv
[i
], bW
)) {
247 creation_flags
&= ~CREATE_NEW_CONSOLE
;
249 else if (argv
[i
][0] == '/' && (argv
[i
][1] == 'i' || argv
[i
][1] == 'I')) {
250 TRACE("/i is ignored\n"); /* FIXME */
252 else if (is_option(argv
[i
], minW
)) {
253 sei
.nShow
= SW_SHOWMINIMIZED
;
255 else if (is_option(argv
[i
], maxW
)) {
256 sei
.nShow
= SW_SHOWMAXIMIZED
;
258 else if (is_option(argv
[i
], lowW
)) {
259 creation_flags
|= IDLE_PRIORITY_CLASS
;
261 else if (is_option(argv
[i
], normalW
)) {
262 creation_flags
|= NORMAL_PRIORITY_CLASS
;
264 else if (is_option(argv
[i
], highW
)) {
265 creation_flags
|= HIGH_PRIORITY_CLASS
;
267 else if (is_option(argv
[i
], realtimeW
)) {
268 creation_flags
|= REALTIME_PRIORITY_CLASS
;
270 else if (is_option(argv
[i
], abovenormalW
)) {
271 creation_flags
|= ABOVE_NORMAL_PRIORITY_CLASS
;
273 else if (is_option(argv
[i
], belownormalW
)) {
274 creation_flags
|= BELOW_NORMAL_PRIORITY_CLASS
;
276 else if (is_option(argv
[i
], separateW
)) {
277 TRACE("/separate is ignored\n"); /* FIXME */
279 else if (is_option(argv
[i
], sharedW
)) {
280 TRACE("/shared is ignored\n"); /* FIXME */
282 else if (is_option(argv
[i
], nodeW
)) {
284 WINE_ERR("you must specify a numa node for the /node option\n");
288 TRACE("/node is ignored\n"); /* FIXME */
292 else if (is_option(argv
[i
], affinityW
))
295 WINE_ERR("you must specify a numa node for the /node option\n");
299 TRACE("/affinity is ignored\n"); /* FIXME */
303 else if (is_option(argv
[i
], wW
) || is_option(argv
[i
], waitW
)) {
304 sei
.fMask
|= SEE_MASK_NOCLOSEPROCESS
;
306 else if (is_option(argv
[i
], helpW
)) {
310 /* Wine extensions */
312 else if (is_option(argv
[i
], unixW
)) {
315 else if (is_option(argv
[i
], progIDOpenW
)) {
320 WINE_ERR("Unknown option '%s'\n", wine_dbgstr_w(argv
[i
]));
328 sei
.lpClass
= argv
[i
++];
329 sei
.fMask
|= SEE_MASK_CLASSNAME
;
333 if (progid_open
|| unix_mode
)
338 sei
.lpFile
= argv
[i
++];
340 args
= build_args( argc
- i
, &argv
[i
] );
341 sei
.lpParameters
= args
;
343 if (unix_mode
|| progid_open
) {
344 LPWSTR (*CDECL wine_get_dos_file_name_ptr
)(LPCSTR
);
345 char* multibyte_unixpath
;
346 int multibyte_unixpath_len
;
348 wine_get_dos_file_name_ptr
= (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
350 if (!wine_get_dos_file_name_ptr
)
351 fatal_string(STRING_UNIXFAIL
);
353 multibyte_unixpath_len
= WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, NULL
, 0, NULL
, NULL
);
354 multibyte_unixpath
= HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len
);
356 WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, multibyte_unixpath
, multibyte_unixpath_len
, NULL
, NULL
);
358 dos_filename
= wine_get_dos_file_name_ptr(multibyte_unixpath
);
360 HeapFree(GetProcessHeap(), 0, multibyte_unixpath
);
363 fatal_string(STRING_UNIXFAIL
);
365 sei
.lpFile
= dos_filename
;
366 if (!sei
.lpDirectory
)
367 sei
.lpDirectory
= parent_directory
= get_parent_dir(dos_filename
);
368 sei
.fMask
&= ~SEE_MASK_FLAG_NO_UI
;
370 if (GetBinaryTypeW(sei
.lpFile
, &binary_type
)) {
372 STARTUPINFOW startup_info
;
373 PROCESS_INFORMATION process_information
;
374 static const WCHAR commandlineformat
[] = {'"','%','s','"','%','s',0};
376 /* 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 */
378 commandline
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(sei
.lpFile
)+3+lstrlenW(sei
.lpParameters
))*sizeof(WCHAR
));
379 swprintf(commandline
, lstrlenW(sei
.lpFile
) + 3 + lstrlenW(sei
.lpParameters
),
380 commandlineformat
, sei
.lpFile
, sei
.lpParameters
);
382 ZeroMemory(&startup_info
, sizeof(startup_info
));
383 startup_info
.cb
= sizeof(startup_info
);
384 startup_info
.lpTitle
= title
;
387 NULL
, /* lpApplicationName */
388 commandline
, /* lpCommandLine */
389 NULL
, /* lpProcessAttributes */
390 NULL
, /* lpThreadAttributes */
391 FALSE
, /* bInheritHandles */
392 creation_flags
, /* dwCreationFlags */
393 NULL
, /* lpEnvironment */
394 sei
.lpDirectory
, /* lpCurrentDirectory */
395 &startup_info
, /* lpStartupInfo */
396 &process_information
/* lpProcessInformation */ ))
398 fatal_string_error(STRING_EXECFAIL
, GetLastError(), sei
.lpFile
);
400 sei
.hProcess
= process_information
.hProcess
;
405 if (!ShellExecuteExW(&sei
))
407 static const WCHAR pathextW
[] = {'P','A','T','H','E','X','T',0};
408 const WCHAR
*filename
= sei
.lpFile
;
409 DWORD size
, filename_len
;
412 size
= GetEnvironmentVariableW(pathextW
, NULL
, 0);
417 env
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
419 fatal_string_error(STRING_EXECFAIL
, ERROR_OUTOFMEMORY
, sei
.lpFile
);
420 GetEnvironmentVariableW(pathextW
, env
, size
);
422 filename_len
= lstrlenW(filename
);
423 name
= HeapAlloc(GetProcessHeap(), 0, (filename_len
+ size
) * sizeof(WCHAR
));
425 fatal_string_error(STRING_EXECFAIL
, ERROR_OUTOFMEMORY
, sei
.lpFile
);
429 while ((ptr
= wcschr(start
, ';')))
437 lstrcpyW(name
, filename
);
438 memcpy(&name
[filename_len
], start
, (ptr
- start
) * sizeof(WCHAR
));
439 name
[filename_len
+ (ptr
- start
)] = 0;
441 if (ShellExecuteExW(&sei
))
443 HeapFree(GetProcessHeap(), 0, name
);
444 HeapFree(GetProcessHeap(), 0, env
);
453 fatal_string_error(STRING_EXECFAIL
, GetLastError(), filename
);
457 HeapFree( GetProcessHeap(), 0, args
);
458 HeapFree( GetProcessHeap(), 0, dos_filename
);
459 HeapFree( GetProcessHeap(), 0, parent_directory
);
461 if (sei
.fMask
& SEE_MASK_NOCLOSEPROCESS
) {
463 WaitForSingleObject(sei
.hProcess
, INFINITE
);
464 GetExitCodeProcess(sei
.hProcess
, &exitcode
);
465 ExitProcess(exitcode
);