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 int wmain (int argc
, WCHAR
*argv
[])
174 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 };
185 static const WCHAR progIDOpenW
[] =
186 { 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
188 memset(&sei
, 0, sizeof(sei
));
189 sei
.cbSize
= sizeof(sei
);
191 sei
.nShow
= SW_SHOWNORMAL
;
192 /* Dunno what these mean, but it looks like winMe's start uses them */
193 sei
.fMask
= SEE_MASK_FLAG_DDEWAIT
|
197 /* Canonical Microsoft commandline flag processing:
198 * flags start with /, are case insensitive,
199 * and may be run together in same word.
201 for (i
=1; i
<argc
; i
++) {
204 if (argv
[i
][0] != '/')
207 /* Unix paths can start with / so we have to assume anything following /U is not a flag */
208 if (unix_mode
|| progid_open
)
211 /* Handle all options in this word */
212 for (ci
=0; argv
[i
][ci
]; ) {
215 switch(argv
[i
][ci
]) {
218 break; /* FIXME: should stop new window from being created */
221 break; /* FIXME: should ignore any changes to current environment */
224 if (argv
[i
][ci
+1] == 'a' || argv
[i
][ci
+1] == 'A')
225 sei
.nShow
= SW_SHOWMAXIMIZED
;
227 sei
.nShow
= SW_SHOWMINIMIZED
;
231 /* sei.nShow = SW_SHOWNORMAL; */
235 if (strncmpiW(&argv
[i
][ci
], unixW
, 4) == 0)
238 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv
[i
]+ci
-1));
244 if (strncmpiW(&argv
[i
][ci
], progIDOpenW
, 17) == 0)
247 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv
[i
]+ci
-1));
253 sei
.fMask
|= SEE_MASK_NOCLOSEPROCESS
;
259 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv
[i
]+ci
-1));
262 /* Skip to next slash */
263 while (argv
[i
][ci
] && (argv
[i
][ci
] != '/'))
272 sei
.lpClass
= argv
[i
++];
273 sei
.fMask
|= SEE_MASK_CLASSNAME
;
276 sei
.lpFile
= argv
[i
++];
278 args
= build_args( argc
- i
, &argv
[i
] );
279 sei
.lpParameters
= args
;
281 if (unix_mode
|| progid_open
) {
282 LPWSTR (*CDECL wine_get_dos_file_name_ptr
)(LPCSTR
);
283 char* multibyte_unixpath
;
284 int multibyte_unixpath_len
;
286 wine_get_dos_file_name_ptr
= (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
288 if (!wine_get_dos_file_name_ptr
)
289 fatal_string(STRING_UNIXFAIL
);
291 multibyte_unixpath_len
= WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, NULL
, 0, NULL
, NULL
);
292 multibyte_unixpath
= HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len
);
294 WideCharToMultiByte(CP_UNIXCP
, 0, sei
.lpFile
, -1, multibyte_unixpath
, multibyte_unixpath_len
, NULL
, NULL
);
296 dos_filename
= wine_get_dos_file_name_ptr(multibyte_unixpath
);
298 HeapFree(GetProcessHeap(), 0, multibyte_unixpath
);
301 fatal_string(STRING_UNIXFAIL
);
303 sei
.lpFile
= dos_filename
;
304 sei
.lpDirectory
= parent_directory
= get_parent_dir(dos_filename
);
305 sei
.fMask
&= ~SEE_MASK_FLAG_NO_UI
;
307 if (GetBinaryTypeW(sei
.lpFile
, &binary_type
)) {
309 STARTUPINFOW startup_info
;
310 PROCESS_INFORMATION process_information
;
311 static WCHAR commandlineformat
[] = {'"','%','s','"','%','s',0};
313 /* 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 */
315 commandline
= HeapAlloc(GetProcessHeap(), 0, (strlenW(sei
.lpFile
)+3+strlenW(sei
.lpParameters
))*sizeof(WCHAR
));
316 sprintfW(commandline
, commandlineformat
, sei
.lpFile
, sei
.lpParameters
);
318 ZeroMemory(&startup_info
, sizeof(startup_info
));
319 startup_info
.cb
= sizeof(startup_info
);
322 NULL
, /* lpApplicationName */
323 commandline
, /* lpCommandLine */
324 NULL
, /* lpProcessAttributes */
325 NULL
, /* lpThreadAttributes */
326 FALSE
, /* bInheritHandles */
327 CREATE_NEW_CONSOLE
, /* dwCreationFlags */
328 NULL
, /* lpEnvironment */
329 sei
.lpDirectory
, /* lpCurrentDirectory */
330 &startup_info
, /* lpStartupInfo */
331 &process_information
/* lpProcessInformation */ ))
333 fatal_string_error(STRING_EXECFAIL
, GetLastError(), sei
.lpFile
);
335 sei
.hProcess
= process_information
.hProcess
;
340 if (!ShellExecuteExW(&sei
))
341 fatal_string_error(STRING_EXECFAIL
, GetLastError(), sei
.lpFile
);
344 HeapFree( GetProcessHeap(), 0, args
);
345 HeapFree( GetProcessHeap(), 0, dos_filename
);
346 HeapFree( GetProcessHeap(), 0, parent_directory
);
348 if (sei
.fMask
& SEE_MASK_NOCLOSEPROCESS
) {
350 WaitForSingleObject(sei
.hProcess
, INFINITE
);
351 GetExitCodeProcess(sei
.hProcess
, &exitcode
);
352 ExitProcess(exitcode
);