wineconsole.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * an application for displaying Win32 console
  3. *
  4. * Copyright 2001, 2002 Eric Pouech
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <stdlib.h>
  23. #include "windef.h"
  24. #include "winbase.h"
  25. #include "wincon.h"
  26. #include "wineconsole_res.h"
  27. #include "wine/debug.h"
  28. WINE_DEFAULT_DEBUG_CHANNEL(console);
  29. int WINAPI wWinMain( HINSTANCE inst, HINSTANCE prev, WCHAR *cmdline, INT show )
  30. {
  31. STARTUPINFOW startup = { sizeof(startup) };
  32. PROCESS_INFORMATION info;
  33. WCHAR *cmd = cmdline;
  34. DWORD exit_code;
  35. static WCHAR default_cmd[] = L"cmd";
  36. FreeConsole(); /* make sure we're not connected to inherited console */
  37. if (!AllocConsole())
  38. {
  39. ERR( "failed to allocate console: %u\n", GetLastError() );
  40. return 1;
  41. }
  42. if (!*cmd) cmd = default_cmd;
  43. startup.dwFlags = STARTF_USESTDHANDLES;
  44. startup.hStdInput = CreateFileW( L"CONIN$", GENERIC_READ | GENERIC_WRITE, 0, NULL,
  45. OPEN_EXISTING, 0, 0 );
  46. startup.hStdOutput = CreateFileW( L"CONOUT$", GENERIC_READ | GENERIC_WRITE, 0, NULL,
  47. OPEN_EXISTING, 0, 0 );
  48. startup.hStdError = startup.hStdOutput;
  49. if (!CreateProcessW( NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info ))
  50. {
  51. WCHAR format[256], *buf;
  52. INPUT_RECORD ir;
  53. DWORD len;
  54. exit_code = GetLastError();
  55. WARN( "CreateProcess failed: %u\n", exit_code );
  56. LoadStringW( GetModuleHandleW( NULL ), IDS_CMD_LAUNCH_FAILED, format, ARRAY_SIZE(format) );
  57. len = wcslen( format ) + wcslen( cmd );
  58. if ((buf = malloc( len * sizeof(WCHAR) )))
  59. {
  60. swprintf( buf, len, format, cmd );
  61. WriteConsoleW( startup.hStdOutput, buf, wcslen(buf), &len, NULL);
  62. while (ReadConsoleInputW( startup.hStdInput, &ir, 1, &len ) && ir.EventType == MOUSE_EVENT);
  63. }
  64. return exit_code;
  65. }
  66. CloseHandle( info.hThread );
  67. WaitForSingleObject( info.hProcess, INFINITE );
  68. return GetExitCodeProcess( info.hProcess, &exit_code ) ? exit_code : GetLastError();
  69. }