Win32 programming hiding console window - c++

I'm learning C++ and I made a new program. I deleted some of my code and now my console window is not hidden. Is there a way to make it hide on startup without them seeing it?

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?

Assuming you're on windows, configure your linker to make a gui-program, not a console program.
VS: Look in Linker ptions on project properties
LINK: add /SUBSYSTEM:WINDOWS
MinGW: -mwindows

#include <windows.h>
#include <iostream>
using namespace std;
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth,0);
}
int main()
{
cout<<"this sentence is visible\n";
Stealth(); //to hide console window
cout<<"this sentence is not visible\n";
system("PAUSE");
return EXIT_SUCCESS;
}

I used to use ShowWindow (GetConsoleWindow(), SW_HIDE); in such case, however if you no need console, so don't create console app project.

As already said, starting the application with console or not is set in the exe. Using gnu compiler the option is -mwindows for no console, for example
g++ -mwindows winapp.c
it seems that the method
#define _WIN32_WINNT 0x0500
#include <wincon.h>
....
case WM_CREATE :
ShowWindow (GetConsoleWindow(), SW_HIDE);
close all parent consoles as well, so if you launch the winapp.exe from a
command line console this will be closed as well!

To literally hide/show the console window on demand, you could use the following functions:
It's possible to hide/show the console by using ShowWindow. GetConsoleWindow retrieves the window handle used by the console.
IsWindowVisible can be used to checked if a window (in that case the console) is visible or not.
#include <Windows.h>
void HideConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}
void ShowConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}
bool IsConsoleVisible()
{
return (::IsWindowVisible(::GetConsoleWindow()) != FALSE);
}

You can create your window minimized. Or paint it outside the visible screen.
But you could also have messed with the window creation flags. If you really messed things up. It is often better to start a new window. (Or restore from a previous version, or the backup).

You can try this
#include <windows.h>
int main() {
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
MessageBox(NULL,"The console Window has been hidden.","Console Hidden",MB_ICONINFORMATION);
return 0;
}
It is part of the win32 API, which you can include using "#include "
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
The first argument tells the program to get the console window that is currently running the program. The second argument passes down the instruction for what you want to do with the window. "SW_HIDE" hides the window, while "SW_SHOW" shows the window.

Related

How to minimize console window?

I am running a C++ console application,
for some period of time,
I want to minimize the window in which my application is running.
for eg. I launch myApp.exe from cmd. Then its launched in new window.
So what are libraries which can minimize the window in which application is running.
Application doesnt have any GUI
I suppose your application is running on Windows (this is not portable across different operating systems).
You have first to get handle of your Console window with GetConsoleWindow() function, then you can use ShowWindow() to hide/show it as required. Ddon't forget to include windows.h:
ShowWindow(GetConsoleWindow(), SW_MINIMIZE);
Instead of SW_MINIMIZE you can use SW_HIDE to completely hide it (but it'll flash visible once when application just started).
Note that if you have control over process creation you can create it as DETACHED_PROCESS: a detached console application does not have a console window. CreateProcess() function has also other workarounds you may be interested in (for example you may create a child process for outputting...)
UPDATE: as follow-up of Patrick's answer you may change the subsystem from Console to Windows and then, if you require to write to console, create a new one using AllocConsole:
if (AllocConsole()) {
printf("Now I can print to console...\n");
FreeConsole();
}
Another option is to change
Properties... | Configuration Properties | Linker | System | Subsystem
from Console to Windows.
However, you then need to add a WinMain() entry point, such as:
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{ int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLine(), &argc);
return Main(argc, argv);
}
assuming unicode. To avoid confusion, I rename the console's wmain() function to something like Main(), as above. Of course printf no longer has a console to write to.

How can I hide the command prompt of an application after it has started?

How do I go about suppressing the command prompt of a c++ application using the mingw compiler AFTER the program has started.. -mwindows works great in the linker settings but I want to be able to toggle it whilst the program is running, is this possible at all?
I am using a text editor and command line so no IDE related answers please.
As far as I know: no, at least not with a single executable. When you open an application in a Windows based console, it will start an instance of conhost.exe in order to provide an environment to your command line application. The console host will run as long as your applications hasn't exited.
It's hard to determine in which circumstances you'll need this behavior. But you could create two application - one which is a simple command line application, and one which has been compiled with -mwindows. The latter could call the first. After the first has exited the second will continue executing.
Note that this will leave the user bewildered, as it seems to him your application has stopped (as the console window has been closed) and a -mwindow compiled application doesn't create any GUI elements.
You can use WinAPI function ShowWindow to hide and show any window. There is a quirk, however - this function accepts an HWND handle as its argument and there is no obviuos way to obtain console HWND. Following is pretty convoluted way to get it:
HWND GetConsoleHwnd(void)
{
#define MY_BUFSIZE 1024 // Buffer size for console window titles.
HWND hwndFound; // This is what is returned to the caller.
TCHAR pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated
// WindowTitle.
TCHAR pszOldWindowTitle[MY_BUFSIZE]; // Contains original
// WindowTitle.
// Fetch current window title.
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
// Format a "unique" NewWindowTitle.
TCHAR * format=_TEXT("%d/%d");
wsprintf(pszNewWindowTitle,format,
GetTickCount(),
GetCurrentProcessId());
// Change current window title.
SetConsoleTitle(pszNewWindowTitle);
// Ensure window title has been updated.
Sleep(40);
// Look for NewWindowTitle.
hwndFound=FindWindow(NULL, pszNewWindowTitle);
// Restore original window title.
SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
Forgive me for this dirty trick, but it works perfectly in my code and is an official way of getting console HWND.
Some programs have to be of a console type. Like Emacs where the same executable can be launched to operate in console (with -nw option) and in GUI.
To hide that console there are lots of methods (including esoteric WSH scripts, or 3rd party utils, like nircmd exec hide notepad.exe) but there is a simple modern portable way:
powershell -c Start-Process -WindowStyle Hidden -FilePath notepad.exe
You can wrap that ugly command into .bat script alias.
PS Use Task Manager to kill hidden Notepad ))

Closing Application without showing console window

I have written very very simple console application which supports some command line options.
If there is no command line argument (it means there is only 1 argument) the application closes without showing black window, currently if you run the code below, because it has no command line arguments, it will close immediately, but it will show the black window for a second, I want to avoid it. So How Can I do it in a simple way?
#include <iostream>
using namespace std;
int main(int argc,char** argv)
{
if (argc==1) return 0;
if (argc!=1)
for (int i=2; i<=argc; i++)
cout << argv[i] << endl;
cin.sync();cin.get();
return 0;
}
You can't escape from console window creation if you create console application.
But you can create win32 application with entry point WinMain and there do not create window, just work as console program.
For the program to run, Windows needs a console window. After the console window is created, the control enters main. It is only inside main that you can check the command line parameters. Which means, it is not possible to do what you want.
You can, however, run the program without showing the console window using the CreateProcess API. So if you can run the console application from another program, then you can check if there are command line arguments and then decide whether you should use CreateProcess to show the console window or not.
To not show the console window using CreateProcess, set the dwFlags parameter of STARTUPINFO to STARTF_USESHOWWINDOW and specify SW_HIDE in the wShowWindow parameter.

Visual C++ Enable Console

I created an Empty Project in Visual C++, but now I need the Console to display debug output.
How can I enable the Console without recreating the project or show the output in the VS output window?
Here's some code you can insert to get a console window in a GUI'd windows app that starts in WinMain. There are other ways to accomplish this but this is the most compact snippet I've found.
//Alloc Console
//print some stuff to the console
//make sure to include #include "stdio.h"
//note, you must use the #include <iostream>/ using namespace std
//to use the iostream... #incldue "iostream.h" didn't seem to work
//in my VC 6
AllocConsole();
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);
printf("Debugging Window:\n");
You can always call AllocConsole in code to create a console for your application, and attach it to the process. FreeConsole will remove the console, detaching the process from it, as well.
If you want all standard output stream data to go to the console, you need to also use SetStdHandle to redirect the output appropriately. Here is a page showing working code to do this full process, including allocating the console and redirecting the output.
You can write to the vs output window with OutputDebugString. http://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx

C/C++ Console Windows WIN32

I know how to "hide" them. I know about FreeConsole(); and then finding the handle and changing it's attributes. However with these methods the window still pops up for a second than goes away. How can I stop it from showing up completely?
Have you considered creating a windows application (with windows subsystem) instead of a console application? That should hide the console window all together.
Try looking at WinMain
I believe you need to be a GUI application to not have the console displayed. Check out /SUBSYSTEM:WINDOWS