Question

Ok this is a continuation from this question: How to make a simple Hello World "invisible" in Windows (C/C++)

People gave me some guidance and here I am with a new qestion:

Aight after doing some research I am stuck again. People on the internet claim that by just creating a win32 application there will be no graphical indications.

Here is the code that does this (I'm pretty sure you already know this but w/e)

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {

//code
return 0;
}

So the code typed inside main is not displayed. I don't really get what kind of code they mean but for example:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {

    while (1) {
    }
}

This program pops a Cmd window just fine.

I've also found that by initializing values at the STARTUPINFO structure like that

STARTUPINFO StartupInfo;
memset(&StartupInfo, 0, sizeof(StartupInfo));
// set the size of the structure
StartupInfo.cb = sizeof(STARTUPINFO);
// tell the application that we are setting the window display 
// information within this structure
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
// set the window display to HIDE
StartupInfo.wShowWindow = SW_HIDE;

would hide the console window. This doesn't work either for me though. I have this feeling that I am missing a major concept here so I need your knowledge guys. I want to create simple .exe with something like a while loop or a simple print that doesn't display a thing. What am I missing?

Was it helpful?

Solution

You have to use a specific compiler option to do this; it's not a property of the code itself. I will assume you are using Visual Studio to compile.

Go to Project Properties > Configuration Properties > Linker > System > SubSystem and set it to Windows. If you do this and run your program and, for example, put your program into an infinite loop, you'll have to kill it from the Task Manager.

I have no idea how to do this on GCC. Gerald has told me in the comments that using --subsystem,windows or -mwindows will do this for GCC. Note that -mwindows links the GDI libraries as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top