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
, msg
, ARRAY_SIZE(msg
)))
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
, msg
, ARRAY_SIZE(msg
)))
114 WINE_ERR("LoadString failed, error %d\n", GetLastError());
120 static void usage(void)
122 fatal_string(STRING_USAGE
);
125 static WCHAR
*build_args( int argc
, WCHAR
**argvW
)
129 static const WCHAR FormatQuotesW
[] = { ' ', '\"', '%', 's', '\"', 0 };
130 static const WCHAR FormatW
[] = { ' ', '%', 's', 0 };
132 for (i
= 0; i
< argc
; i
++ )
134 wlen
+= strlenW(argvW
[i
]) + 1;
135 if (strchrW(argvW
[i
], ' '))
138 ret
= HeapAlloc( GetProcessHeap(), 0, wlen
*sizeof(WCHAR
) );
141 for (i
= 0, p
= ret
; i
< argc
; i
++ )
143 if (strchrW(argvW
[i
], ' '))
144 p
+= sprintfW(p
, FormatQuotesW
, argvW
[i
]);
146 p
+= sprintfW(p
, FormatW
, argvW
[i
]);
151 static WCHAR
*get_parent_dir(WCHAR
* path
)
157 last_slash
= strrchrW( path
, '\\' );
158 if (last_slash
== NULL
)
161 len
= last_slash
- path
+ 1;
163 result
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
164 CopyMemory(result
, path
, (len
-1)*sizeof(WCHAR
));
165 result
[len
-1] = '\0';
170 static BOOL
is_option(const WCHAR
* arg
, const WCHAR
* opt
)
172 return CompareStringW(LOCALE_USER_DEFAULT
, NORM_IGNORECASE
,
173 arg
, -1, opt
, -1) == CSTR_EQUAL
;
176 int wmain (int argc
, WCHAR
*argv
[])
178 SHELLEXECUTEINFOW sei
;
179 DWORD creation_flags
;
182 BOOL unix_mode
= FALSE
;
183 BOOL progid_open
= FALSE
;
185 WCHAR
*dos_filename
= NULL
;
186 WCHAR
*parent_directory
= NULL
;
189 static const WCHAR bW
[] = { '/', 'b', 0 };
190 static const WCHAR minW
[] = { '/', 'm', 'i', 'n', 0 };
191 static const WCHAR maxW
[] = { '/', 'm', 'a', 'x', 0 };
192 static const WCHAR lowW
[] = { '/', 'l', 'o', 'w', 0 };
193 static const WCHAR normalW
[] = { '/', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
194 static const WCHAR highW
[] = { '/', 'h', 'i', 'g', 'h', 0 };
195 static const WCHAR realtimeW
[] = { '/', 'r', 'e', 'a', 'l', 't', 'i', 'm', 'e', 0 };
196 static const WCHAR abovenormalW
[] = { '/', 'a', 'b', 'o', 'v', 'e', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
197 static const WCHAR belownormalW
[] = { '/', 'b', 'e', 'l', 'o', 'w', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
198 static const WCHAR separateW
[] = { '/', 's', 'e', 'p', 'a', 'r', 'a', 't', 'e', 0 };
199 static const WCHAR sharedW
[] = { '/', 's', 'h', 'a', 'r', 'e', 'd', 0 };
200 static const WCHAR nodeW
[] = { '/', 'n', 'o', 'd', 'e', 0 };
201 static const WCHAR affinityW
[] = { '/', 'a', 'f', 'f', 'i', 'n', 'i', 't', 'y', 0 };
202 static const WCHAR wW
[] = { '/', 'w', 0 };
203 static const WCHAR waitW
[] = { '/', 'w', 'a', 'i', 't', 0 };
204 static const WCHAR helpW
[] = { '/', '?', 0 };
205 static const WCHAR unixW
[] = { '/', 'u', 'n', 'i', 'x', 0 };
206 static const WCHAR progIDOpenW
[] =
207 { '/', 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
208 static const WCHAR openW
[] = { 'o', 'p', 'e', 'n', 0 };
209 static const WCHAR cmdW
[] = { 'c', 'm', 'd', '.', 'e', 'x', 'e', 0 };
211 memset(&sei
, 0, sizeof(sei
));
212 sei
.cbSize
= sizeof(sei
);
214 sei
.nShow
= SW_SHOWNORMAL
;
215 /* Dunno what these mean, but it looks like winMe's start uses them */
216 sei
.fMask
= SEE_MASK_FLAG_DDEWAIT
|
218 sei
.lpDirectory
= NULL
;
219 creation_flags
= CREATE_NEW_CONSOLE
;
221 /* Canonical Microsoft commandline flag processing:
222 * flags start with / and are case insensitive.
224 for (i
=1; i
<argc
; i
++) {
225 /* parse first quoted argument as console title */
226 if (!title
&& argv
[i
][0] == '"') {
230 if (argv
[i
][0] != '/')
233 /* Unix paths can start with / so we have to assume anything following /unix is not a flag */
234 if (unix_mode
|| progid_open
)
237 if (argv
[i
][0] == '/' && (argv
[i
][1] == 'd' || argv
[i
][1] == 'D')) {
239 /* The start directory was concatenated to the option */
240 sei
.lpDirectory
= argv
[i
]+2;
241 else if (i
+1 == argc
) {
242 WINE_ERR("you must specify a directory path for the /d option\n");
245 sei
.lpDirectory
= argv
[++i
];
247 else if (is_option(argv
[i
], bW
)) {
248 creation_flags
&= ~CREATE_NEW_CONSOLE
;
250 else if (argv
[i
][0] == '/' && (argv
[i
][1] == 'i' || argv
[i
][1] == 'I')) {
251 TRACE("/i is ignored\n"); /* FIXME */
253 else if (is_option(argv
[i
], minW
)) {
254 sei
.nShow
= SW_SHOWMINIMIZED
;
256 else if (is_option(argv
[i
], maxW
)) {
257 sei
.nShow
= SW_SHOWMAXIMIZED
;
259 else if (is_option(argv
[i
], lowW
)) {
260 creation_flags
|= IDLE_PRIORITY_CLASS
;
262 else if (is_option(argv
[i
], normalW
)) {
263 creation_flags
|= NORMAL_PRIORITY_CLASS
;
265 else if (is_option(argv
[i
], highW
)) {
266 creation_flags
|= HIGH_PRIORITY_CLASS
;
268 else if (is_option(argv
[i
], realtimeW
)) {
269 creation_flags
|= REALTIME_PRIORITY_CLASS
;
271 else if (is_option(argv
[i
], abovenormalW
)) {
272 creation_flags
|= ABOVE_NORMAL_PRIORITY_CLASS
;
274 else if (is_option(argv
[i
], belownormalW
)) {
275 creation_flags
|= BELOW_NORMAL_PRIORITY_CLASS
;
277 else if (is_option(argv
[i
], separateW
)) {
278 TRACE("/separate is ignored\n"); /* FIXME */
280 else if (is_option(argv
[i
], sharedW
)) {
281 TRACE("/shared is ignored\n"); /* FIXME */
283 else if (is_option(argv
[i
], nodeW
)) {
285 WINE_ERR("you must specify a numa node for the /node option\n");
289 TRACE("/node is ignored\n"); /* FIXME */
293 else if (is_option(argv
[i
], affinityW
))
296 WINE_ERR("you must specify a numa node for the /node option\n");
300 TRACE("/affinity is ignored\n"); /* FIXME */
304 else if (is_option(argv
[i
], wW
) || is_option(argv
[i
], waitW
)) {
305 sei
.fMask
|= SEE_MASK_NOCLOSEPROCESS
;
307 else if (is_option(argv
[i
], helpW
)) {
311 /* Wine extensions */
313 else if (is_option(argv
[i
], unixW
)) {
316 else if (is_option(argv
[i
], progIDOpenW
)) {
321 WINE_ERR("Unknown option '%s'\n", wine_dbgstr_w(argv
[i
]));
329 sei
.lpClass
= argv
[i
++];
330 sei
.fMask
|= SEE_MASK_CLASSNAME
;
334 if (progid_open
|| unix_mode
)
339 sei
.lpFile
= argv
[i
++];
341 args
= build_args( argc
- i
, &argv
[i
] );
342 sei
.lpParameters
= args
;
344 if (unix_mode
|| progid_open
) {
345 LPWSTR (*CDECL wine_get_dos_file_name_ptr
)(LPCSTR
);
346 char* multibyte_unixpath
;
347 int multibyte_unixpath_len
;
349 wine_get_dos_file_name_ptr
= (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
351 if (!wine_get_dos_file_name_ptr
)
352 fatal_string(STRING_UNIXFAIL
);
354 multibyte_unixpath_len
= WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, NULL
, 0, NULL
, NULL
);
355 multibyte_unixpath
= HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len
);
357 WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, multibyte_unixpath
, multibyte_unixpath_len
, NULL
, NULL
);
359 dos_filename
= wine_get_dos_file_name_ptr(multibyte_unixpath
);
361 HeapFree(GetProcessHeap(), 0, multibyte_unixpath
);
364 fatal_string(STRING_UNIXFAIL
);
366 sei
.lpFile
= dos_filename
;
367 if (!sei
.lpDirectory
)
368 sei
.lpDirectory
= parent_directory
= get_parent_dir(dos_filename
);
369 sei
.fMask
&= ~SEE_MASK_FLAG_NO_UI
;
371 if (GetBinaryTypeW(sei
.lpFile
, &binary_type
)) {
373 STARTUPINFOW startup_info
;
374 PROCESS_INFORMATION process_information
;
375 static const WCHAR commandlineformat
[] = {'"','%','s','"','%','s',0};
377 /* 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 */
379 commandline
= HeapAlloc(GetProcessHeap(), 0, (strlenW(sei
.lpFile
)+3+strlenW(sei
.lpParameters
))*sizeof(WCHAR
));
380 sprintfW(commandline
, 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
))
406 fatal_string_error(STRING_EXECFAIL
, GetLastError(), sei
.lpFile
);
409 HeapFree( GetProcessHeap(), 0, args
);
410 HeapFree( GetProcessHeap(), 0, dos_filename
);
411 HeapFree( GetProcessHeap(), 0, parent_directory
);
413 if (sei
.fMask
& SEE_MASK_NOCLOSEPROCESS
) {
415 WaitForSingleObject(sei
.hProcess
, INFINITE
);
416 GetExitCodeProcess(sei
.hProcess
, &exitcode
);
417 ExitProcess(exitcode
);