From b527b573cf2fd25085c0ea2dcd3c83a7182a1b74 Mon Sep 17 00:00:00 2001 From: Henrik "Henke37" Andersson Date: Sat, 25 May 2019 21:09:25 +0200 Subject: WIN32: Let the PE header control showing the console. This applies DRY to the console config and avoids junk code changes seen by git. --- backends/platform/sdl/win32/win32.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'backends/platform') diff --git a/backends/platform/sdl/win32/win32.cpp b/backends/platform/sdl/win32/win32.cpp index aada5b4e68..30ebf9d1d2 100644 --- a/backends/platform/sdl/win32/win32.cpp +++ b/backends/platform/sdl/win32/win32.cpp @@ -74,9 +74,23 @@ void OSystem_Win32::init() { OSystem_SDL::init(); } +WORD GetCurrentSubsystem() { + // HMODULE is the module base address. And the PIMAGE_DOS_HEADER is located at the beginning. + PIMAGE_DOS_HEADER EXEHeader = (PIMAGE_DOS_HEADER)GetModuleHandle(NULL); + assert(EXEHeader->e_magic == IMAGE_DOS_SIGNATURE); + // PIMAGE_NT_HEADERS is bitness dependant. + // Conveniently, since it's for our own process, it's always the correct bitness. + // IMAGE_NT_HEADERS has to be found using a byte offset from the EXEHeader, + // which requires the ugly cast. + PIMAGE_NT_HEADERS PEHeader = (PIMAGE_NT_HEADERS)(((char*)EXEHeader) + EXEHeader->e_lfanew); + assert(PEHeader->Signature == IMAGE_NT_SIGNATURE); + return PEHeader->OptionalHeader.Subsystem; +} + void OSystem_Win32::initBackend() { - // Console window is enabled by default on Windows - ConfMan.registerDefault("console", true); + // The console window is enabled for the console subsystem, + // since Windows already creates the console window for us + ConfMan.registerDefault("console", GetCurrentSubsystem() == IMAGE_SUBSYSTEM_WINDOWS_CUI); // Enable or disable the window console window if (ConfMan.getBool("console")) { -- cgit v1.2.3