From 54238f9bf1bed8c879a1f12c0bfb7af469f2fc6c Mon Sep 17 00:00:00 2001 From: Willi Schinmeyer Date: Thu, 17 May 2012 14:11:03 +0200 Subject: [PATCH] Suppressing the console window by using WinMain - two variants --- radiant/main.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++ radiant/radiant.vcproj | 6 ++--- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/radiant/main.cpp b/radiant/main.cpp index ede1426f..ace951a1 100644 --- a/radiant/main.cpp +++ b/radiant/main.cpp @@ -51,6 +51,65 @@ bool g_bBuildList = false; int g_argc; char** g_argv; +// ============================================================================= +// Windows WinMain() wrapper for main() +// used in Release Builds to suppress the Console +#if defined(_WIN32) + +// contains __getmainargs() +//#include +#include + +int main(int argc, char* argv[]); + +int CALLBACK WinMain( + __in HINSTANCE hInstance, + __in HINSTANCE hPrevInstance, + __in LPSTR lpCmdLine, + __in int nCmdShow +){ + if(false) { + // while this works, it's not exactly nice + __argc = 3; + // load msvcrt.dll - we need an internal function from it + HMODULE crtlib = LoadLibrary("msvcrt.dll"); + if(crtlib == NULL) { + // do error handling here? + return 0; + } + + // retrieve pointer to function + struct fake_startupinfo { + int newmode; + }; + typedef int(*GetMainArgsPtr)(int*, char***, char***, int, fake_startupinfo*); + GetMainArgsPtr __getmainargs = (GetMainArgsPtr) GetProcAddress(crtlib, "__getmainargs"); + if(__getmainargs == NULL){ + FreeLibrary(crtlib); + return 0; + } + + fake_startupinfo startinfo; + startinfo.newmode = 0; //this sets _newmode - what does that do? + int argc; + char** argv; + char** envp; + int argret = __getmainargs(&argc, &argv, &envp, 0, &startinfo); //0: no wildcard expanding + FreeLibrary(crtlib); + if(argret < 0) { + // might want to handle this error here? + return 0; + } + return main(argc, argv); + } + else { + // This, on the other hand, seems very nice - but just like the first version I'm not sure it always works. + return main(__argc, __argv); + } +} + +#endif + // ============================================================================= // Splash screen diff --git a/radiant/radiant.vcproj b/radiant/radiant.vcproj index 851666b8..0d5c3f63 100644 --- a/radiant/radiant.vcproj +++ b/radiant/radiant.vcproj @@ -1,7 +1,7 @@ - +