Win32 Programming Hiding Console Window

Win32 programming hiding console window

If you're writing a console program and you want to disconnect your program from the console it started with, then call FreeConsole. Ultimately, you probably won't be satisfied with what that function really does, but that's the literal answer to the question you asked.

If you're writing a program that you never want to have a console in the first place, then configure your project so that it is not a console program. "Consoleness" is a property of the EXE file. The OS reads that setting and decides whether to allocate a console for your program before any of your code ever runs, so you can't control it within the program. Sometimes a non-console program is called a "GUI program," so you might look for a choice between "console" and "GUI" in the configuration options of your development environment. Setting it to GUI doesn't require that you have any user interface at all, though. The setting merely controls whether your program starts with a console.

If you're trying to write a program that can sometimes have a console and sometimes not, then please see an earlier question, Can one executable be both a console and GUI app?

How to COMPLETELY hide console in a Win32 program?

Something is weird with your compiler settings. (You did not specify your compiler so I'm assuming MSVC)

The WinMain entry point function from your example is used by GUI applications (Linker switch /SUBSYSTEM:WINDOWS) and those applications don't get a console unless you call AllocConsole.

If you link with /SUBSYSTEM:CONSOLE then Windows will create a console for the process before your code is executed but a normal entry point for those applications is the plain old main function.

Are you forcing the entry point with the /ENTRY switch? Either way, make sure the /SUBSYSTEM switch and the entry point function definition are compatible and match the type of application you want to create (GUI or console)

How to hide the console window in a Win32 project using Visual Studio 2010

Yes, when you build with UNICODE in effect then the entrypoint takes a Unicode string for the command line argument. So that requires a different declaration:

  int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
LPWSTR lpCmdLine, int nShowCmd)

Or you can use #include <tchar.h> so it works either way, not much point to it these days:

  int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
LPTSTR lpCmdLine, int nCmdShow)

Hide the console window on an exe

If you're on Windows, and use MinGW GCC (or Clang with GCC's libraries), pass -mwindows flag to the linker.

HowTo hide Console Window with WinAPI?

Just call FreeConsole() get rid of it and AllocConsole() to create a new one.

Hide console window in remote process

You can use the CREATE_NO_WINDOW flag to start a console application without a console window. It's not the same as it being hidden, but it sounds like it's what you want.

Hiding the console on a windows socket

Use CREATE_NO_WINDOW flag (sixth parameter in CreateProcess) to create a hidden console window.

Note that the second parameter in CreateProcess should be a writable buffer. Example:

char buf[MAX_PATH];
strcpy_s(buf, "cmd.exe");
CreateProcess(NULL, buf, NULL, NULL, TRUE, CREATE_NO_WINDOW,
NULL, NULL, &ini_processo, &processo_info);


To create a window program without console, use WinMain entry point instead of main.

int APIENTRY WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int show)
{
MessageBox(0, "Test", 0, 0);
return 0;
}

In Visual Studio, set linker option to Windows subsystem.

In MinGW, use -mwindows linker option:

gcc myfile.c -mwindows -o myfile.exe

In Code::Blocks, enable GUI application, see image:

Sample Image


To keep the program running you may use while(Sleep(1000)); (with caution) or use a dummy message loop such as:

MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}


Related Topics



Leave a reply



Submit