iKlsR
iKlsR

Reputation: 2672

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.

Upvotes: 1

Views: 1228

Answers (3)

gavenkoa
gavenkoa

Reputation: 48713

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 ))

Upvotes: 0

Basilevs
Basilevs

Reputation: 23896

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.

Upvotes: 1

Zeta
Zeta

Reputation: 105876

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.

Upvotes: 2

Related Questions