2 * an application for displaying Win32 console
4 * Copyright 2001, 2002 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wineconsole_res.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(console
);
35 int WINAPI
wWinMain( HINSTANCE inst
, HINSTANCE prev
, WCHAR
*cmdline
, INT show
)
37 STARTUPINFOW startup
= { sizeof(startup
) };
38 PROCESS_INFORMATION info
;
42 static WCHAR default_cmd
[] = L
"cmd";
44 if (!*cmd
) cmd
= default_cmd
;
46 if (!CreateProcessW( NULL
, cmd
, NULL
, NULL
, FALSE
, CREATE_NEW_CONSOLE
, NULL
, NULL
, &startup
, &info
))
48 HANDLE hStdInput
, hStdOutput
;
49 WCHAR format
[256], *buf
;
53 exit_code
= GetLastError();
54 WARN( "CreateProcess '%ls' failed: %lu\n", cmd
, exit_code
);
56 /* create a new console to display error messages in it */
57 FreeConsole(); /* make sure we're not connected to any console */
60 ERR( "failed to allocate console: %lu\n", GetLastError() );
64 hStdInput
= CreateFileW( L
"CONIN$", GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
65 OPEN_EXISTING
, 0, 0 );
66 hStdOutput
= CreateFileW( L
"CONOUT$", GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
67 OPEN_EXISTING
, 0, 0 );
69 LoadStringW( GetModuleHandleW( NULL
), IDS_CMD_LAUNCH_FAILED
, format
, ARRAY_SIZE(format
) );
70 len
= wcslen( format
) + wcslen( cmd
);
71 if ((buf
= malloc( len
* sizeof(WCHAR
) )))
73 swprintf( buf
, len
, format
, cmd
);
74 WriteConsoleW( hStdOutput
, buf
, wcslen(buf
), &len
, NULL
);
75 while (ReadConsoleInputW( hStdInput
, &ir
, 1, &len
) && ir
.EventType
== MOUSE_EVENT
);
80 CloseHandle( info
.hThread
);
81 WaitForSingleObject( info
.hProcess
, INFINITE
);
82 return GetExitCodeProcess( info
.hProcess
, &exit_code
) ? exit_code
: GetLastError();