31

I'm writing an application which dumps some diagnostics to the standard output.

I'd like to have the application work this way:

  • If it is run from a standalone command prompt (via cmd.exe) or has standard output redirected/piped to a file, exit cleanly as soon as it finished,
  • Otherwise (if it is run from a window and the console window is spawned automagically), then additionally wait for a keypress before exiting (to let the user read the diagnostics) before the window disappears

How do I make that distinction? I suspect that examining the parent process could be a way but I'm not really into WinAPI, hence the question.

I'm on MinGW GCC.

Borealid
  • 95,191
  • 9
  • 106
  • 122
Kos
  • 70,399
  • 25
  • 169
  • 233
  • possible duplicate of [Do i own my console or i inherited it from my parent?](http://stackoverflow.com/questions/6048690/do-i-own-my-console-or-i-inherited-it-from-my-parent) – Rob Kennedy Jan 25 '12 at 20:36

5 Answers5

34

You can use GetConsoleWindow, GetWindowThreadProcessId and GetCurrentProcessId methods.

  1. First you must retrieve the current handle of the console window using the GetConsoleWindow function.

  2. Then you get the process owner of the handle of the console window.

  3. Finally you compare the returned PID against the PID of your application.

Check this sample (VS C++)

#include "stdafx.h"
#include <iostream>
using namespace std;
#if       _WIN32_WINNT < 0x0500
  #undef  _WIN32_WINNT
  #define _WIN32_WINNT   0x0500
#endif
#include <windows.h>
#include "Wincon.h" 

int _tmain(int argc, _TCHAR* argv[])
{   
    HWND consoleWnd = GetConsoleWindow();
    DWORD dwProcessId;
    GetWindowThreadProcessId(consoleWnd, &dwProcessId);
    if (GetCurrentProcessId()==dwProcessId)
    {
        cout << "I have my own console, press enter to exit" << endl;
        cin.get();
    }
    else
    {
        cout << "This Console is not mine, good bye" << endl;   
    }


    return 0;
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Works like a charm in all cases I could think of, on MinGW too. Perfect, thanks! – Kos Jan 25 '12 at 20:40
  • Glad to help you, just be aware which this method doesn't check the redirection of the console. for that you can use the [`GetStdHandle`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx) and [`GetFileInformationByHandle`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364952%28v=vs.85%29.aspx) functions. – RRUZ Jan 25 '12 at 20:44
  • Won't `GetWindowThreadProcessId` return the ID of CSRSS, not the program that first started running in that window? – Rob Kennedy Jan 25 '12 at 20:44
  • @RobKennedy I don't understeand your comment the `GetWindowThreadProcessId` return the PID of the process which create the window if you are running the app from the cmd will return the PID of the cmd.exe otherwise will return the pid of the App. – RRUZ Jan 25 '12 at 20:55
  • 3
    Another method that works on both Windows and Linux is to get the Environment variable "PROMPT". If it doesn't come back NULL, you were run from the console. Then use the isatty/_isattty method to catch redirection in William Pursell's answer, which is simpler than checking for fail from GetFileInformationByHandle which I think is Windows only. – ChocoBilly Jun 19 '15 at 03:49
  • 3
    @RobKennedy: `GetWindowThreadProcessId()` will not report the process ID of `csrss.exe` (or even `conhost.exe`). Even though that is the process that manages the console *object* and its I/O buffers, that is not the process that creates the actual console *window*. – Remy Lebeau Jun 24 '16 at 01:57
8

The typical test is:

if( isatty( STDOUT_FILENO )) {
        /* this is a terminal */
}
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 3
    I think that on Windows it's `_isatty`, declared in – Drew Dormann Jan 25 '12 at 20:22
  • Does this differentiate between the program having a console window of its own and the program having a console window that it inherited from its parent process? – Rob Kennedy Jan 25 '12 at 20:34
  • 1
    I don't think this is what the OP is after, I think the OP wants a way to determine whether the program was run in an existing terminal window, or whether a terminal window was spawned just for the process. – dreamlax Jan 25 '12 at 20:35
  • 1
    For those that need it, the Windows way is: if (_isatty(_fileno(stdout))) – ChocoBilly Jun 18 '15 at 23:12
  • Does not work when you use console and redirect output to file. – TarmoPikaro Apr 03 '20 at 21:47
  • @TarmoPikaro In that situation, it correctly identifies that stdout is not a tty. If you want to check that stdin is a tty, use `isatty(STDIN_FILENO)` – William Pursell Apr 03 '20 at 22:01
  • I wanted to differentiate console versus windows application, I guess we have slightly different targets then. – TarmoPikaro Apr 04 '20 at 08:54
7

I needed this in C#. Here's the translation:

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcessId();

[DllImport("user32.dll")]
static extern int GetWindowThreadProcessId(IntPtr hWnd, ref IntPtr ProcessId);

static int Main(string[] args)
{
    IntPtr hConsole = GetConsoleWindow();
    IntPtr hProcessId = IntPtr.Zero;
    GetWindowThreadProcessId(hConsole, ref hProcessId);

    if (GetCurrentProcessId().Equals(hProcessId))
    {
        Console.WriteLine("I have my own console, press any key to exit");
        Console.ReadKey();
    }
    else
        Console.WriteLine("This console is not mine, good bye");

    return 0;
}
SMD
  • 87
  • 1
  • 1
3

I18N guru Michael Kaplan of Microsoft provided a series of methods on his blog that let you check a bunch of things on the console, including whether or not the console has been redirected.

They're written in C#, but porting to C or C++ would be very straightforward, as it's all done with calls to the Win32 API.

Community
  • 1
  • 1
Michael Madsen
  • 54,231
  • 8
  • 72
  • 83
1

After trying quite many different api's and calls, I've found out only one working approach:

// #include <io.h>
bool isConsole = isatty(fileno(stdin));

Stdout cannot be used as you can run console application and redirect output to file using >log.txt switch. Suspect it's also possible to redirect input as well, but it's quite rarely used functionality when running console applications.

Prince Owen
  • 1,225
  • 12
  • 20
TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62