console_wrapper_windows.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 = ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
  63. SetConsoleMode(stdout_handle, out_mode);
  64. // Find main executable name and check if it exist.
  65. static PCWSTR exe_renames[] = {
  66. L".console.exe",
  67. L"_console.exe",
  68. L" console.exe",
  69. L"console.exe",
  70. nullptr,
  71. };
  72. bool rename_found = false;
  73. for (int i = 0; exe_renames[i]; i++) {
  74. PWSTR c = StrRStrIW(exe_name, nullptr, exe_renames[i]);
  75. if (c) {
  76. CopyMemory(c, L".exe", sizeof(WCHAR) * 5);
  77. rename_found = true;
  78. break;
  79. }
  80. }
  81. if (!rename_found) {
  82. wprintf(L"Invalid wrapper executable name.\n");
  83. return -1;
  84. }
  85. DWORD file_attrib = GetFileAttributesW(exe_name);
  86. if (file_attrib == INVALID_FILE_ATTRIBUTES || (file_attrib & FILE_ATTRIBUTE_DIRECTORY)) {
  87. wprintf(L"Main executable %ls not found.\n", exe_name);
  88. return -1;
  89. }
  90. // Create job to monitor process tree.
  91. HANDLE job_handle = CreateJobObjectW(nullptr, nullptr);
  92. if (!job_handle) {
  93. wprintf(L"CreateJobObject failed, error %d\n", GetLastError());
  94. return -1;
  95. }
  96. HANDLE io_port_handle = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 1);
  97. if (!io_port_handle) {
  98. wprintf(L"CreateIoCompletionPort failed, error %d\n", GetLastError());
  99. return -1;
  100. }
  101. JOBOBJECT_ASSOCIATE_COMPLETION_PORT compl_port;
  102. ZeroMemory(&compl_port, sizeof(compl_port));
  103. compl_port.CompletionKey = job_handle;
  104. compl_port.CompletionPort = io_port_handle;
  105. if (!SetInformationJobObject(job_handle, JobObjectAssociateCompletionPortInformation, &compl_port, sizeof(compl_port))) {
  106. wprintf(L"SetInformationJobObject(AssociateCompletionPortInformation) failed, error %d\n", GetLastError());
  107. return -1;
  108. }
  109. JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
  110. ZeroMemory(&jeli, sizeof(jeli));
  111. jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
  112. if (!SetInformationJobObject(job_handle, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli))) {
  113. wprintf(L"SetInformationJobObject(ExtendedLimitInformation) failed, error %d\n", GetLastError());
  114. return -1;
  115. }
  116. // Start the main process.
  117. PROCESS_INFORMATION pi;
  118. ZeroMemory(&pi, sizeof(pi));
  119. STARTUPINFOW si;
  120. ZeroMemory(&si, sizeof(si));
  121. si.cb = sizeof(si);
  122. si.dwFlags = STARTF_USESTDHANDLES;
  123. si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  124. si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  125. si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  126. WCHAR new_command_line[32767];
  127. _snwprintf_s(new_command_line, 32767, _TRUNCATE, L"%ls %ls", exe_name, PathGetArgsW(GetCommandLineW()));
  128. if (!CreateProcessW(nullptr, new_command_line, nullptr, nullptr, true, CREATE_SUSPENDED, nullptr, nullptr, &si, &pi)) {
  129. wprintf(L"CreateProcess failed, error %d\n", GetLastError());
  130. return -1;
  131. }
  132. if (!AssignProcessToJobObject(job_handle, pi.hProcess)) {
  133. wprintf(L"AssignProcessToJobObject failed, error %d\n", GetLastError());
  134. return -1;
  135. }
  136. ResumeThread(pi.hThread);
  137. CloseHandle(pi.hThread);
  138. // Wait until main process and all of its children are finished.
  139. DWORD completion_code = 0;
  140. ULONG_PTR completion_key = 0;
  141. LPOVERLAPPED overlapped = nullptr;
  142. while (GetQueuedCompletionStatus(io_port_handle, &completion_code, &completion_key, &overlapped, INFINITE)) {
  143. if ((HANDLE)completion_key == job_handle && completion_code == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO) {
  144. break;
  145. }
  146. }
  147. CloseHandle(job_handle);
  148. CloseHandle(io_port_handle);
  149. // Get exit code of the main process.
  150. DWORD exit_code = 0;
  151. GetExitCodeProcess(pi.hProcess, &exit_code);
  152. CloseHandle(pi.hProcess);
  153. return exit_code;
  154. }
  155. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  156. return main(0, nullptr);
  157. }