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
)
77 static const WCHAR colonsW
[] = { ':', ' ', 0 };
78 static const WCHAR newlineW
[] = { '\n', 0 };
82 status
= FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error_code
, 0, (LPWSTR
) & lpMsgBuf
, 0, NULL
);
85 WINE_ERR("FormatMessage failed\n");
89 LocalFree((HLOCAL
) lpMsgBuf
);
95 static void fatal_string_error(int which
, DWORD error_code
)
99 if (!LoadStringW(GetModuleHandle(NULL
), which
,
100 msg
, sizeof(msg
)/sizeof(WCHAR
)))
101 WINE_ERR("LoadString failed, error %d\n", GetLastError());
103 fatal_error(msg
, error_code
);
106 static void fatal_string(int which
)
110 if (!LoadStringW(GetModuleHandle(NULL
), which
,
111 msg
, sizeof(msg
)/sizeof(WCHAR
)))
112 WINE_ERR("LoadString failed, error %d\n", GetLastError());
118 static void usage(void)
120 fatal_string(STRING_USAGE
);
123 static void license(void)
125 fatal_string(STRING_LICENSE
);
128 static WCHAR
*build_args( int argc
, WCHAR
**argvW
)
132 static const WCHAR FormatQuotesW
[] = { ' ', '\"', '%', 's', '\"', 0 };
133 static const WCHAR FormatW
[] = { ' ', '%', 's', 0 };
135 for (i
= 0; i
< argc
; i
++ )
137 wlen
+= strlenW(argvW
[i
]) + 1;
138 if (strchrW(argvW
[i
], ' '))
141 ret
= HeapAlloc( GetProcessHeap(), 0, wlen
*sizeof(WCHAR
) );
144 for (i
= 0, p
= ret
; i
< argc
; i
++ )
146 if (strchrW(argvW
[i
], ' '))
147 p
+= sprintfW(p
, FormatQuotesW
, argvW
[i
]);
149 p
+= sprintfW(p
, FormatW
, argvW
[i
]);
154 static WCHAR
*get_parent_dir(WCHAR
* path
)
160 last_slash
= strrchrW( path
, '\\' );
161 if (last_slash
== NULL
)
164 len
= last_slash
- path
+ 1;
166 result
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
167 CopyMemory(result
, path
, (len
-1)*sizeof(WCHAR
));
168 result
[len
-1] = '\0';
173 int wmain (int argc
, WCHAR
*argv
[])
175 SHELLEXECUTEINFOW sei
;
179 WCHAR
*dos_filename
= NULL
;
180 WCHAR
*parent_directory
= NULL
;
183 static const WCHAR openW
[] = { 'o', 'p', 'e', 'n', 0 };
184 static const WCHAR unixW
[] = { 'u', 'n', 'i', 'x', 0 };
186 memset(&sei
, 0, sizeof(sei
));
187 sei
.cbSize
= sizeof(sei
);
189 sei
.nShow
= SW_SHOWNORMAL
;
190 /* Dunno what these mean, but it looks like winMe's start uses them */
191 sei
.fMask
= SEE_MASK_FLAG_DDEWAIT
|
195 /* Canonical Microsoft commandline flag processing:
196 * flags start with /, are case insensitive,
197 * and may be run together in same word.
199 for (i
=1; i
<argc
; i
++) {
202 if (argv
[i
][0] != '/')
205 /* Unix paths can start with / so we have to assume anything following /U is not a flag */
209 /* Handle all options in this word */
210 for (ci
=0; argv
[i
][ci
]; ) {
213 switch(argv
[i
][ci
]) {
217 break; /* notreached */
220 if (argv
[i
][ci
+1] == 'a' || argv
[i
][ci
+1] == 'A')
221 sei
.nShow
= SW_SHOWMAXIMIZED
;
223 sei
.nShow
= SW_SHOWMINIMIZED
;
227 /* sei.nShow = SW_SHOWNORMAL; */
231 if (strncmpiW(&argv
[i
][ci
], unixW
, 4) == 0)
234 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv
[i
]+ci
-1));
240 sei
.fMask
|= SEE_MASK_NOCLOSEPROCESS
;
243 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv
[i
]+ci
-1));
246 /* Skip to next slash */
247 while (argv
[i
][ci
] && (argv
[i
][ci
] != '/'))
255 sei
.lpFile
= argv
[i
++];
257 args
= build_args( argc
- i
, &argv
[i
] );
258 sei
.lpParameters
= args
;
261 LPWSTR (*wine_get_dos_file_name_ptr
)(LPCSTR
);
262 char* multibyte_unixpath
;
263 int multibyte_unixpath_len
;
265 wine_get_dos_file_name_ptr
= (void*)GetProcAddress(GetModuleHandle("KERNEL32"), "wine_get_dos_file_name");
267 if (!wine_get_dos_file_name_ptr
)
268 fatal_string(STRING_UNIXFAIL
);
270 multibyte_unixpath_len
= WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, NULL
, 0, NULL
, NULL
);
271 multibyte_unixpath
= HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len
);
273 WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, multibyte_unixpath
, multibyte_unixpath_len
, NULL
, NULL
);
275 dos_filename
= wine_get_dos_file_name_ptr(multibyte_unixpath
);
277 HeapFree(GetProcessHeap(), 0, multibyte_unixpath
);
280 fatal_string(STRING_UNIXFAIL
);
282 sei
.lpFile
= dos_filename
;
283 sei
.lpDirectory
= parent_directory
= get_parent_dir(dos_filename
);
285 if (GetBinaryTypeW(sei
.lpFile
, &binary_type
)) {
287 STARTUPINFOW startup_info
;
288 PROCESS_INFORMATION process_information
;
289 static WCHAR commandlineformat
[] = {'"','%','s','"','%','s',0};
291 /* 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 */
293 commandline
= HeapAlloc(GetProcessHeap(), 0, (strlenW(sei
.lpFile
)+3+strlenW(sei
.lpParameters
))*sizeof(WCHAR
));
294 sprintfW(commandline
, commandlineformat
, sei
.lpFile
, sei
.lpParameters
);
296 ZeroMemory(&startup_info
, sizeof(startup_info
));
297 startup_info
.cb
= sizeof(startup_info
);
300 NULL
, /* lpApplicationName */
301 commandline
, /* lpCommandLine */
302 NULL
, /* lpProcessAttributes */
303 NULL
, /* lpThreadAttributes */
304 FALSE
, /* bInheritHandles */
305 CREATE_NEW_CONSOLE
, /* dwCreationFlags */
306 NULL
, /* lpEnvironment */
307 sei
.lpDirectory
, /* lpCurrentDirectory */
308 &startup_info
, /* lpStartupInfo */
309 &process_information
/* lpProcessInformation */ ))
311 fatal_string_error(STRING_EXECFAIL
, GetLastError());
313 sei
.hProcess
= process_information
.hProcess
;
316 else if (!ShellExecuteExW(&sei
))
317 fatal_string_error(STRING_EXECFAIL
, GetLastError());
319 HeapFree( GetProcessHeap(), 0, args
);
320 HeapFree( GetProcessHeap(), 0, dos_filename
);
321 HeapFree( GetProcessHeap(), 0, parent_directory
);
323 if (sei
.fMask
& SEE_MASK_NOCLOSEPROCESS
) {
325 WaitForSingleObject(sei
.hProcess
, INFINITE
);
326 GetExitCodeProcess(sei
.hProcess
, &exitcode
);
327 ExitProcess(exitcode
);