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 WCHAR
*build_args( int argc
, WCHAR
**argvW
)
131 static const WCHAR FormatQuotesW
[] = { ' ', '\"', '%', 's', '\"', 0 };
132 static const WCHAR FormatW
[] = { ' ', '%', 's', 0 };
134 for (i
= 0; i
< argc
; i
++ )
136 wlen
+= strlenW(argvW
[i
]) + 1;
137 if (strchrW(argvW
[i
], ' '))
140 ret
= HeapAlloc( GetProcessHeap(), 0, wlen
*sizeof(WCHAR
) );
143 for (i
= 0, p
= ret
; i
< argc
; i
++ )
145 if (strchrW(argvW
[i
], ' '))
146 p
+= sprintfW(p
, FormatQuotesW
, argvW
[i
]);
148 p
+= sprintfW(p
, FormatW
, argvW
[i
]);
153 static WCHAR
*get_parent_dir(WCHAR
* path
)
159 last_slash
= strrchrW( path
, '\\' );
160 if (last_slash
== NULL
)
163 len
= last_slash
- path
+ 1;
165 result
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
166 CopyMemory(result
, path
, (len
-1)*sizeof(WCHAR
));
167 result
[len
-1] = '\0';
172 static BOOL
is_option(const WCHAR
* arg
, const WCHAR
* opt
)
174 return CompareStringW(LOCALE_USER_DEFAULT
, NORM_IGNORECASE
,
175 arg
, -1, opt
, -1) == CSTR_EQUAL
;
178 int wmain (int argc
, WCHAR
*argv
[])
180 SHELLEXECUTEINFOW sei
;
181 DWORD creation_flags
;
184 BOOL unix_mode
= FALSE
;
185 BOOL progid_open
= FALSE
;
187 WCHAR
*dos_filename
= NULL
;
188 WCHAR
*parent_directory
= NULL
;
191 static const WCHAR bW
[] = { '/', 'b', 0 };
192 static const WCHAR minW
[] = { '/', 'm', 'i', 'n', 0 };
193 static const WCHAR maxW
[] = { '/', 'm', 'a', 'x', 0 };
194 static const WCHAR lowW
[] = { '/', 'l', 'o', 'w', 0 };
195 static const WCHAR normalW
[] = { '/', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
196 static const WCHAR highW
[] = { '/', 'h', 'i', 'g', 'h', 0 };
197 static const WCHAR realtimeW
[] = { '/', 'r', 'e', 'a', 'l', 't', 'i', 'm', 'e', 0 };
198 static const WCHAR abovenormalW
[] = { '/', 'a', 'b', 'o', 'v', 'e', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
199 static const WCHAR belownormalW
[] = { '/', 'b', 'e', 'l', 'o', 'w', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
200 static const WCHAR separateW
[] = { '/', 's', 'e', 'p', 'a', 'r', 'a', 't', 'e', 0 };
201 static const WCHAR sharedW
[] = { '/', 's', 'h', 'a', 'r', 'e', 'd', 0 };
202 static const WCHAR nodeW
[] = { '/', 'n', 'o', 'd', 'e', 0 };
203 static const WCHAR affinityW
[] = { '/', 'a', 'f', 'f', 'i', 'n', 'i', 't', 'y', 0 };
204 static const WCHAR wW
[] = { '/', 'w', 0 };
205 static const WCHAR waitW
[] = { '/', 'w', 'a', 'i', 't', 0 };
206 static const WCHAR helpW
[] = { '/', '?', 0 };
207 static const WCHAR unixW
[] = { '/', 'u', 'n', 'i', 'x', 0 };
208 static const WCHAR progIDOpenW
[] =
209 { '/', 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
210 static const WCHAR openW
[] = { 'o', 'p', 'e', 'n', 0 };
211 static const WCHAR cmdW
[] = { 'c', 'm', 'd', '.', 'e', 'x', 'e', 0 };
213 memset(&sei
, 0, sizeof(sei
));
214 sei
.cbSize
= sizeof(sei
);
216 sei
.nShow
= SW_SHOWNORMAL
;
217 /* Dunno what these mean, but it looks like winMe's start uses them */
218 sei
.fMask
= SEE_MASK_FLAG_DDEWAIT
|
220 sei
.lpDirectory
= NULL
;
221 creation_flags
= CREATE_NEW_CONSOLE
;
223 /* Canonical Microsoft commandline flag processing:
224 * flags start with / and are case insensitive.
226 for (i
=1; i
<argc
; i
++) {
227 /* parse first quoted argument as console title */
228 if (!title
&& argv
[i
][0] == '"') {
232 if (argv
[i
][0] != '/')
235 /* Unix paths can start with / so we have to assume anything following /unix is not a flag */
236 if (unix_mode
|| progid_open
)
239 if (argv
[i
][0] == '/' && (argv
[i
][1] == 'd' || argv
[i
][1] == 'D')) {
241 /* The start directory was concatenated to the option */
242 sei
.lpDirectory
= argv
[i
]+2;
243 else if (i
+1 == argc
) {
244 WINE_ERR("you must specify a directory path for the /d option\n");
247 sei
.lpDirectory
= argv
[++i
];
249 else if (is_option(argv
[i
], bW
)) {
250 creation_flags
&= ~CREATE_NEW_CONSOLE
;
252 else if (argv
[i
][0] == '/' && (argv
[i
][1] == 'i' || argv
[i
][1] == 'I')) {
253 TRACE("/i is ignored\n"); /* FIXME */
255 else if (is_option(argv
[i
], minW
)) {
256 sei
.nShow
= SW_SHOWMINIMIZED
;
258 else if (is_option(argv
[i
], maxW
)) {
259 sei
.nShow
= SW_SHOWMAXIMIZED
;
261 else if (is_option(argv
[i
], lowW
)) {
262 creation_flags
|= IDLE_PRIORITY_CLASS
;
264 else if (is_option(argv
[i
], normalW
)) {
265 creation_flags
|= NORMAL_PRIORITY_CLASS
;
267 else if (is_option(argv
[i
], highW
)) {
268 creation_flags
|= HIGH_PRIORITY_CLASS
;
270 else if (is_option(argv
[i
], realtimeW
)) {
271 creation_flags
|= REALTIME_PRIORITY_CLASS
;
273 else if (is_option(argv
[i
], abovenormalW
)) {
274 creation_flags
|= ABOVE_NORMAL_PRIORITY_CLASS
;
276 else if (is_option(argv
[i
], belownormalW
)) {
277 creation_flags
|= BELOW_NORMAL_PRIORITY_CLASS
;
279 else if (is_option(argv
[i
], separateW
)) {
280 TRACE("/separate is ignored\n"); /* FIXME */
282 else if (is_option(argv
[i
], sharedW
)) {
283 TRACE("/shared is ignored\n"); /* FIXME */
285 else if (is_option(argv
[i
], nodeW
)) {
287 WINE_ERR("you must specify a numa node for the /node option\n");
291 TRACE("/node is ignored\n"); /* FIXME */
295 else if (is_option(argv
[i
], affinityW
))
298 WINE_ERR("you must specify a numa node for the /node option\n");
302 TRACE("/affinity is ignored\n"); /* FIXME */
306 else if (is_option(argv
[i
], wW
) || is_option(argv
[i
], waitW
)) {
307 sei
.fMask
|= SEE_MASK_NOCLOSEPROCESS
;
309 else if (is_option(argv
[i
], helpW
)) {
313 /* Wine extensions */
315 else if (is_option(argv
[i
], unixW
)) {
318 else if (is_option(argv
[i
], progIDOpenW
)) {
323 WINE_ERR("Unknown option '%s'\n", wine_dbgstr_w(argv
[i
]));
331 sei
.lpClass
= argv
[i
++];
332 sei
.fMask
|= SEE_MASK_CLASSNAME
;
336 if (progid_open
|| unix_mode
)
341 sei
.lpFile
= argv
[i
++];
343 args
= build_args( argc
- i
, &argv
[i
] );
344 sei
.lpParameters
= args
;
346 if (unix_mode
|| progid_open
) {
347 LPWSTR (*CDECL wine_get_dos_file_name_ptr
)(LPCSTR
);
348 char* multibyte_unixpath
;
349 int multibyte_unixpath_len
;
351 wine_get_dos_file_name_ptr
= (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
353 if (!wine_get_dos_file_name_ptr
)
354 fatal_string(STRING_UNIXFAIL
);
356 multibyte_unixpath_len
= WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, NULL
, 0, NULL
, NULL
);
357 multibyte_unixpath
= HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len
);
359 WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, multibyte_unixpath
, multibyte_unixpath_len
, NULL
, NULL
);
361 dos_filename
= wine_get_dos_file_name_ptr(multibyte_unixpath
);
363 HeapFree(GetProcessHeap(), 0, multibyte_unixpath
);
366 fatal_string(STRING_UNIXFAIL
);
368 sei
.lpFile
= dos_filename
;
369 if (!sei
.lpDirectory
)
370 sei
.lpDirectory
= parent_directory
= get_parent_dir(dos_filename
);
371 sei
.fMask
&= ~SEE_MASK_FLAG_NO_UI
;
373 if (GetBinaryTypeW(sei
.lpFile
, &binary_type
)) {
375 STARTUPINFOW startup_info
;
376 PROCESS_INFORMATION process_information
;
377 static const WCHAR commandlineformat
[] = {'"','%','s','"','%','s',0};
379 /* 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 */
381 commandline
= HeapAlloc(GetProcessHeap(), 0, (strlenW(sei
.lpFile
)+3+strlenW(sei
.lpParameters
))*sizeof(WCHAR
));
382 sprintfW(commandline
, commandlineformat
, sei
.lpFile
, sei
.lpParameters
);
384 ZeroMemory(&startup_info
, sizeof(startup_info
));
385 startup_info
.cb
= sizeof(startup_info
);
386 startup_info
.lpTitle
= title
;
389 NULL
, /* lpApplicationName */
390 commandline
, /* lpCommandLine */
391 NULL
, /* lpProcessAttributes */
392 NULL
, /* lpThreadAttributes */
393 FALSE
, /* bInheritHandles */
394 creation_flags
, /* dwCreationFlags */
395 NULL
, /* lpEnvironment */
396 sei
.lpDirectory
, /* lpCurrentDirectory */
397 &startup_info
, /* lpStartupInfo */
398 &process_information
/* lpProcessInformation */ ))
400 fatal_string_error(STRING_EXECFAIL
, GetLastError(), sei
.lpFile
);
402 sei
.hProcess
= process_information
.hProcess
;
407 if (!ShellExecuteExW(&sei
))
408 fatal_string_error(STRING_EXECFAIL
, GetLastError(), sei
.lpFile
);
411 HeapFree( GetProcessHeap(), 0, args
);
412 HeapFree( GetProcessHeap(), 0, dos_filename
);
413 HeapFree( GetProcessHeap(), 0, parent_directory
);
415 if (sei
.fMask
& SEE_MASK_NOCLOSEPROCESS
) {
417 WaitForSingleObject(sei
.hProcess
, INFINITE
);
418 GetExitCodeProcess(sei
.hProcess
, &exitcode
);
419 ExitProcess(exitcode
);