console_wrapper_windows.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**************************************************************************/
  2. /* console_wrapper_windows.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include <windows.h>
  31. #include <shlwapi.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
  35. #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4
  36. #endif
  37. int main(int argc, char *argv[]) {
  38. // Get executable name.
  39. WCHAR exe_name[32767] = {};
  40. if (!GetModuleFileNameW(nullptr, exe_name, 32767)) {
  41. wprintf(L"GetModuleFileName failed, error %d\n", GetLastError());
  42. return -1;
  43. }
  44. // Get product name from the resources and set console title.
  45. DWORD ver_info_handle = 0;
  46. DWORD ver_info_size = GetFileVersionInfoSizeW(exe_name, &ver_info_handle);
  47. if (ver_info_size > 0) {
  48. LPBYTE ver_info = (LPBYTE)malloc(ver_info_size);
  49. if (ver_info) {
  50. if (GetFileVersionInfoW(exe_name, ver_info_handle, ver_info_size, ver_info)) {
  51. LPCWSTR text_ptr = nullptr;
  52. UINT text_size = 0;
  53. if (VerQueryValueW(ver_info, L"\\StringFileInfo\\040904b0\\ProductName", (void **)&text_ptr, &text_size) && (text_size > 0)) {
  54. SetConsoleTitleW(text_ptr);
  55. }
  56. }
  57. free(ver_info);
  58. }
  59. }
  60. // Enable virtual terminal sequences processing.
  61. HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  62. DWORD out_mode = 0;
  63. GetConsoleMode(stdout_handle, &out_mode);
  64. out_mode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
  65. SetConsoleMode(stdout_handle, out_mode);
  66. // Find main executable name and check if it exist.
  67. static PCWSTR exe_renames[] = {
  68. L".console.exe",
  69. L"_console.exe",
  70. L" console.exe",
  71. L"console.exe",
  72. nullptr,
  73. };
  74. bool rename_found = false;
  75. for (int i = 0; exe_renames[i]; i++) {
  76. PWSTR c = StrRStrIW(exe_name, nullptr, exe_renames[i]);
  77. if (c) {
  78. CopyMemory(c, L".exe", sizeof(WCHAR) * 5);
  79. rename_found = true;
  80. break;
  81. }
  82. }
  83. if (!rename_found) {
  84. wprintf(L"Invalid wrapper executable name.\n");
  85. return -1;
  86. }
  87. DWORD file_attrib = GetFileAttributesW(exe_name);
  88. if (file_attrib == INVALID_FILE_ATTRIBUTES || (file_attrib & FILE_ATTRIBUTE_DIRECTORY)) {
  89. wprintf(L"Main executable %ls not found.\n", exe_name);
  90. return -1;
  91. }
  92. // Create job to monitor process tree.
  93. HANDLE job_handle = CreateJobObjectW(nullptr, nullptr);
  94. if (!job_handle) {
  95. wprintf(L"CreateJobObject failed, error %d\n", GetLastError());
  96. return -1;
  97. }
  98. HANDLE io_port_handle = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 1);
  99. if (!io_port_handle) {
  100. wprintf(L"CreateIoCompletionPort failed, error %d\n", GetLastError());
  101. return -1;
  102. }
  103. JOBOBJECT_ASSOCIATE_COMPLETION_PORT compl_port;
  104. ZeroMemory(&compl_port, sizeof(compl_port));
  105. compl_port.CompletionKey = job_handle;
  106. compl_port.CompletionPort = io_port_handle;
  107. if (!SetInformationJobObject(job_handle, JobObjectAssociateCompletionPortInformation, &compl_port, sizeof(compl_port))) {
  108. wprintf(L"SetInformationJobObject(AssociateCompletionPortInformation) failed, error %d\n", GetLastError());
  109. return -1;
  110. }
  111. JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
  112. ZeroMemory(&jeli, sizeof(jeli));
  113. jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
  114. if (!SetInformationJobObject(job_handle, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli))) {
  115. wprintf(L"SetInformationJobObject(ExtendedLimitInformation) failed, error %d\n", GetLastError());
  116. return -1;
  117. }
  118. // Start the main process.
  119. PROCESS_INFORMATION pi;
  120. ZeroMemory(&pi, sizeof(pi));
  121. STARTUPINFOW si;
  122. ZeroMemory(&si, sizeof(si));
  123. si.cb = sizeof(si);
  124. si.dwFlags = STARTF_USESTDHANDLES;
  125. si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  126. si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  127. si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  128. WCHAR new_command_line[32767];
  129. _snwprintf_s(new_command_line, 32767, _TRUNCATE, L"%ls %ls", exe_name, PathGetArgsW(GetCommandLineW()));
  130. if (!CreateProcessW(nullptr, new_command_line, nullptr, nullptr, true, CREATE_SUSPENDED, nullptr, nullptr, &si, &pi)) {
  131. wprintf(L"CreateProcess failed, error %d\n", GetLastError());
  132. return -1;
  133. }
  134. if (!AssignProcessToJobObject(job_handle, pi.hProcess)) {
  135. wprintf(L"AssignProcessToJobObject failed, error %d\n", GetLastError());
  136. return -1;
  137. }
  138. ResumeThread(pi.hThread);
  139. CloseHandle(pi.hThread);
  140. // Wait until main process and all of its children are finished.
  141. DWORD completion_code = 0;
  142. ULONG_PTR completion_key = 0;
  143. LPOVERLAPPED overlapped = nullptr;
  144. while (GetQueuedCompletionStatus(io_port_handle, &completion_code, &completion_key, &overlapped, INFINITE)) {
  145. if ((HANDLE)completion_key == job_handle && completion_code == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO) {
  146. break;
  147. }
  148. }
  149. CloseHandle(job_handle);
  150. CloseHandle(io_port_handle);
  151. // Get exit code of the main process.
  152. DWORD exit_code = 0;
  153. GetExitCodeProcess(pi.hProcess, &exit_code);
  154. CloseHandle(pi.hProcess);
  155. return exit_code;
  156. }
  157. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  158. return main(0, nullptr);
  159. }