| 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 | |
| 31 | #include <windows.h> |
| 32 | |
| 33 | #include <shlwapi.h> |
| 34 | #include <cstdio> |
| 35 | #include <cstdlib> |
| 36 | |
| 37 | #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING |
| 38 | #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x4 |
| 39 | #endif |
| 40 | |
| 41 | int main(int argc, char *argv[]) { |
| 42 | // Get executable name. |
| 43 | WCHAR exe_name[32767] = {}; |
| 44 | if (!GetModuleFileNameW(nullptr, exe_name, 32767)) { |
| 45 | wprintf(L"GetModuleFileName failed, error %d\n", GetLastError()); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | // Get product name from the resources and set console title. |
| 50 | DWORD ver_info_handle = 0; |
| 51 | DWORD ver_info_size = GetFileVersionInfoSizeW(exe_name, &ver_info_handle); |
| 52 | if (ver_info_size > 0) { |
| 53 | LPBYTE ver_info = (LPBYTE)malloc(ver_info_size); |
| 54 | if (ver_info) { |
| 55 | if (GetFileVersionInfoW(exe_name, ver_info_handle, ver_info_size, ver_info)) { |
| 56 | LPCWSTR text_ptr = nullptr; |
| 57 | UINT text_size = 0; |
| 58 | if (VerQueryValueW(ver_info, L"\\StringFileInfo\\040904b0\\ProductName", (void **)&text_ptr, &text_size) && (text_size > 0)) { |
| 59 | SetConsoleTitleW(text_ptr); |
| 60 | } |
| 61 | } |
| 62 | free(ver_info); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Enable virtual terminal sequences processing. |
| 67 | HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 68 | DWORD out_mode = 0; |
| 69 | GetConsoleMode(stdout_handle, &out_mode); |
| 70 | out_mode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING; |
| 71 | SetConsoleMode(stdout_handle, out_mode); |
| 72 | |
| 73 | // Find main executable name and check if it exist. |
| 74 | static PCWSTR exe_renames[] = { |
| 75 | L".console.exe", |
| 76 | L"_console.exe", |
| 77 | L" console.exe", |
| 78 | L"console.exe", |
| 79 | nullptr, |
| 80 | }; |
| 81 | |
| 82 | bool rename_found = false; |
| 83 | for (int i = 0; exe_renames[i]; i++) { |
| 84 | PWSTR c = StrRStrIW(exe_name, nullptr, exe_renames[i]); |
| 85 | if (c) { |
| 86 | CopyMemory(c, L".exe", sizeof(WCHAR) * 5); |
| 87 | rename_found = true; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | if (!rename_found) { |
| 92 | wprintf(L"Invalid wrapper executable name.\n"); |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | DWORD file_attrib = GetFileAttributesW(exe_name); |
| 97 | if (file_attrib == INVALID_FILE_ATTRIBUTES || (file_attrib & FILE_ATTRIBUTE_DIRECTORY)) { |
| 98 | wprintf(L"Main executable %ls not found.\n", exe_name); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | // Create job to monitor process tree. |
| 103 | HANDLE job_handle = CreateJobObjectW(nullptr, nullptr); |
| 104 | if (!job_handle) { |
| 105 | wprintf(L"CreateJobObject failed, error %d\n", GetLastError()); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | HANDLE io_port_handle = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 1); |
| 110 | if (!io_port_handle) { |
| 111 | wprintf(L"CreateIoCompletionPort failed, error %d\n", GetLastError()); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | JOBOBJECT_ASSOCIATE_COMPLETION_PORT compl_port; |
| 116 | ZeroMemory(&compl_port, sizeof(compl_port)); |
| 117 | compl_port.CompletionKey = job_handle; |
| 118 | compl_port.CompletionPort = io_port_handle; |
| 119 | |
| 120 | if (!SetInformationJobObject(job_handle, JobObjectAssociateCompletionPortInformation, &compl_port, sizeof(compl_port))) { |
| 121 | wprintf(L"SetInformationJobObject(AssociateCompletionPortInformation) failed, error %d\n", GetLastError()); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli; |
| 126 | ZeroMemory(&jeli, sizeof(jeli)); |
| 127 | jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; |
| 128 | |
| 129 | if (!SetInformationJobObject(job_handle, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli))) { |
| 130 | wprintf(L"SetInformationJobObject(ExtendedLimitInformation) failed, error %d\n", GetLastError()); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | // Start the main process. |
| 135 | PROCESS_INFORMATION pi; |
| 136 | ZeroMemory(&pi, sizeof(pi)); |
| 137 | |
| 138 | STARTUPINFOW si; |
| 139 | ZeroMemory(&si, sizeof(si)); |
| 140 | si.cb = sizeof(si); |
| 141 | si.dwFlags = STARTF_USESTDHANDLES; |
| 142 | si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 143 | si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| 144 | si.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| 145 | |
| 146 | WCHAR new_command_line[32767]; |
| 147 | _snwprintf_s(new_command_line, 32767, _TRUNCATE, L"%ls %ls", exe_name, PathGetArgsW(GetCommandLineW())); |
| 148 | |
| 149 | if (!CreateProcessW(nullptr, new_command_line, nullptr, nullptr, true, CREATE_SUSPENDED, nullptr, nullptr, &si, &pi)) { |
| 150 | wprintf(L"CreateProcess failed, error %d\n", GetLastError()); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | if (!AssignProcessToJobObject(job_handle, pi.hProcess)) { |
| 155 | wprintf(L"AssignProcessToJobObject failed, error %d\n", GetLastError()); |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | ResumeThread(pi.hThread); |
| 160 | CloseHandle(pi.hThread); |
| 161 | |
| 162 | // Wait until main process and all of its children are finished. |
| 163 | DWORD completion_code = 0; |
| 164 | ULONG_PTR completion_key = 0; |
| 165 | LPOVERLAPPED overlapped = nullptr; |
| 166 | |
| 167 | while (GetQueuedCompletionStatus(io_port_handle, &completion_code, &completion_key, &overlapped, INFINITE)) { |
| 168 | if ((HANDLE)completion_key == job_handle && completion_code == JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO) { |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | CloseHandle(job_handle); |
| 174 | CloseHandle(io_port_handle); |
| 175 | |
| 176 | // Get exit code of the main process. |
| 177 | DWORD exit_code = 0; |
| 178 | GetExitCodeProcess(pi.hProcess, &exit_code); |
| 179 | |
| 180 | CloseHandle(pi.hProcess); |
| 181 | |
| 182 | return exit_code; |
| 183 | } |
| 184 | |
| 185 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { |
| 186 | return main(0, nullptr); |
| 187 | } |