KulaGGin
KulaGGin

Reputation: 1259

How to make a program that takes command line arguments, outputs to console and can be executed in silent mode without showing console window?

I'm trying to make a program that could be executed from the command line terminal but also by other programs(well, in my case it's by Visual Studio when I run another project: Debugging-Command in project settings), and support either normal mode where it prints out to the console: either the command line terminal console it was run from, or a new command line terminal window if it's executed by other programs, and also support silent mode when it's executed by other programs: don't show the console window at all.

Here's the requirements in a list:

  1. A program can take command line arguments.
  2. A program can be started either from a command line terminal or by another program with command line arguments.
  3. If a program is started from a console, it uses the console window from which it was started.
  4. If a program is started by another program, it prints output to its' own console.
  5. A program can be passed a --silent command line argument, in which case it doesn't spawn its' own console when ran from another program. I don't want to see the console even for 1 frame.

So I start with a console application in Visual Studio and it always spawns the console window no matter what. The solution to it I found here on SO is this: #pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup"). Now when the program starts, it doesn't spawn the console window by itself. So what I did is I spawn the console and redirect stdout to it with: AllocConsole(); freopen("CONOUT$", "w", stdout);.

The last problem that I have now is that when this program is launched from the console, it always spawns its' own console and there is no way to use the console from which the program was launched. I tried to use GetConsoleWindow() and _isatty(_fileno(stdout)) that I also found here on SO, but they both return false when run the program from a console window. Here's the full program from my attempt:

#include <cstdio>
#include <string>
#include <windows.h>
#include <io.h>
#include <iostream>

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

void main(int argc, char* argv[]) {
    bool SilentMode{};

    if(argc == 1)
        SilentMode = false;
    else
        SilentMode = std::string(argv[1]) == "silent";

    bool IsRunningFromInsideConsole = GetConsoleWindow();
    bool IsRunningFromInsideConsole2 = _isatty(_fileno(stdout));
    
    if(!SilentMode && !(IsRunningFromInsideConsole || IsRunningFromInsideConsole2)) {
        AllocConsole();
        freopen("CONOUT$", "w", stdout);
    }

    std::cout << "IsRunningFromInsideConsole:" << IsRunningFromInsideConsole << std::endl;
    std::cout << "IsRunningFromInsideConsole2:" << IsRunningFromInsideConsole2 << std::endl;

    std::getchar();
}

So, this will always spawn its' own console even when it's ran from inside the console window.

Upvotes: -5

Views: 66

Answers (0)

Related Questions