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
;
267 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv
[i
]+ci
-1));
270 /* Skip to next slash */
271 while (argv
[i
][ci
] && (argv
[i
][ci
] != '/'))
280 sei
.lpClass
= argv
[i
++];
281 sei
.fMask
|= SEE_MASK_CLASSNAME
;
284 sei
.lpFile
= argv
[i
++];
286 args
= build_args( argc
- i
, &argv
[i
] );
287 sei
.lpParameters
= args
;
289 if (unix_mode
|| progid_open
) {
290 LPWSTR (*CDECL wine_get_dos_file_name_ptr
)(LPCSTR
);
291 char* multibyte_unixpath
;
292 int multibyte_unixpath_len
;
294 wine_get_dos_file_name_ptr
= (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
296 if (!wine_get_dos_file_name_ptr
)
297 fatal_string(STRING_UNIXFAIL
);
299 multibyte_unixpath_len
= WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, NULL
, 0, NULL
, NULL
);
300 multibyte_unixpath
= HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len
);
302 WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, multibyte_unixpath
, multibyte_unixpath_len
, NULL
, NULL
);
304 dos_filename
= wine_get_dos_file_name_ptr(multibyte_unixpath
);
306 HeapFree(GetProcessHeap(), 0, multibyte_unixpath
);
309 fatal_string(STRING_UNIXFAIL
);
311 sei
.lpFile
= dos_filename
;
312 sei
.lpDirectory
= parent_directory
= get_parent_dir(dos_filename
);
313 sei
.fMask
&= ~SEE_MASK_FLAG_NO_UI
;
315 if (GetBinaryTypeW(sei
.lpFile
, &binary_type
)) {
317 STARTUPINFOW startup_info
;
318 PROCESS_INFORMATION process_information
;
319 static WCHAR commandlineformat
[] = {'"','%','s','"','%','s',0};
321 /* 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 */
323 commandline
= HeapAlloc(GetProcessHeap(), 0, (strlenW(sei
.lpFile
)+3+strlenW(sei
.lpParameters
))*sizeof(WCHAR
));
324 sprintfW(commandline
, commandlineformat
, sei
.lpFile
, sei
.lpParameters
);
326 ZeroMemory(&startup_info
, sizeof(startup_info
));
327 startup_info
.cb
= sizeof(startup_info
);
330 NULL
, /* lpApplicationName */
331 commandline
, /* lpCommandLine */
332 NULL
, /* lpProcessAttributes */
333 NULL
, /* lpThreadAttributes */
334 FALSE
, /* bInheritHandles */
335 CREATE_NEW_CONSOLE
, /* dwCreationFlags */
336 NULL
, /* lpEnvironment */
337 sei
.lpDirectory
, /* lpCurrentDirectory */
338 &startup_info
, /* lpStartupInfo */
339 &process_information
/* lpProcessInformation */ ))
341 fatal_string_error(STRING_EXECFAIL
, GetLastError(), sei
.lpFile
);
343 sei
.hProcess
= process_information
.hProcess
;
348 if (!ShellExecuteExW(&sei
))
349 fatal_string_error(STRING_EXECFAIL
, GetLastError(), sei
.lpFile
);
352 HeapFree( GetProcessHeap(), 0, args
);
353 HeapFree( GetProcessHeap(), 0, dos_filename
);
354 HeapFree( GetProcessHeap(), 0, parent_directory
);
356 if (sei
.fMask
& SEE_MASK_NOCLOSEPROCESS
) {
358 WaitForSingleObject(sei
.hProcess
, INFINITE
);
359 GetExitCodeProcess(sei
.hProcess
, &exitcode
);
360 ExitProcess(exitcode
);