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