• Advertisement
Sign in to follow this  

Windows console app

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, In windows, when your main function is a main() and not a WinMain() function, you get a console. I am using GLFW to manage my windows. So I don't need a WinMain function. Is there a way to make the console dissapear? ex. You double click the exe, you never see the console, and then the window GLFW creates appears. I want to make the console never appear. Is this possible? Thanks, Slaru

Share this post


Link to post
Share on other sites
Advertisement
Short answer: no
Long answer: if you really really want to, I'm sure it's possible (through various API calls and events), but there's no easy way that I've ever seen

Console apps are meant to be just that: consoles. The fact that you can create windows is really just a side-effect of the fact that it's still a win32 app.

Most developers end up creating a stub WinMain that wraps whatever framework they're using. SDL, for example, provides a wrapper that creates the graphics context then calls your main() function so you can start the game.

Share this post


Link to post
Share on other sites
If I recall correctly, simply putting your main function in WinMain (and setting a windows, rather than console, subsystem) is all you need to do. WinMain does not automatically create a window.

Share this post


Link to post
Share on other sites
The reason I wanted to was so I can totally not rely on Windows, for easy cross-platform porting (GLFW is cross-platform). If it is very complicated, then I will just use windows and WinMain. But if there is a way not too complicated, then that would be nice.

Share this post


Link to post
Share on other sites
You will need to write some windows specific code. You can either achieve this as Sneftel suggested (/SUBSYTEM:Windows) and winmain, or you can just call
FreeConsole
Just use
#ifdef _WIN32
#endif
to ensure your windows specific code only compiles under windows

Share this post


Link to post
Share on other sites
You could try something like:

int main(void);

#ifdef _WIN32 // If compiling for 32-bit Windows

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
return main(); // Note: If you have
// int main(int argc, char **argv)
// then you will have to parse lpCmdLine and fill in argc and argv yourself
}

#endif

int main(void)
{
...
}


This ensures that, if compiling under Windows, WinMain() will be in the entry point and it will call main(), and if compiling under a different OS, main() will simply be the entry point.

Share this post


Link to post
Share on other sites
I never really saw the point of preferring WinMain over main. WinMain gets its parameters via GetModuleHandle(NULL) and GetCommandLine() anyway.

Share this post


Link to post
Share on other sites
Short answer: Yes.

I make GLUT apps all the time using main(). If you are using Visual Studio, add this line to your source:

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

You could do it in the project properties, but this line is easier.

Share this post


Link to post
Share on other sites
Thanks mrbreakit. That worked perfectly! Exactly what I wanted.

Slaru

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement