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 FreeConsole(); /* make sure we're not connected to inherited console */
47 ERR( "failed to allocate console: %u\n", GetLastError() );
51 if (!*cmd
) cmd
= default_cmd
;
53 startup
.dwFlags
= STARTF_USESTDHANDLES
;
54 startup
.hStdInput
= CreateFileW( L
"CONIN$", GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
55 OPEN_EXISTING
, 0, 0 );
56 startup
.hStdOutput
= CreateFileW( L
"CONOUT$", GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
57 OPEN_EXISTING
, 0, 0 );
58 startup
.hStdError
= startup
.hStdOutput
;
60 if (!CreateProcessW( NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
))
62 WCHAR format
[256], *buf
;
65 exit_code
= GetLastError();
66 WARN( "CreateProcess failed: %u\n", exit_code
);
67 LoadStringW( GetModuleHandleW( NULL
), IDS_CMD_LAUNCH_FAILED
, format
, ARRAY_SIZE(format
) );
68 len
= wcslen( format
) + wcslen( cmd
);
69 if ((buf
= malloc( len
* sizeof(WCHAR
) )))
71 swprintf( buf
, len
, format
, cmd
);
72 WriteConsoleW( startup
.hStdOutput
, buf
, wcslen(buf
), &len
, NULL
);
73 while (ReadConsoleInputW( startup
.hStdInput
, &ir
, 1, &len
) && ir
.EventType
== MOUSE_EVENT
);
78 CloseHandle( info
.hThread
);
79 WaitForSingleObject( info
.hProcess
, INFINITE
);
80 return GetExitCodeProcess( info
.hProcess
, &exit_code
) ? exit_code
: GetLastError();