Thread: having two console windows in c++ application

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    having two console windows in c++ application

    as the title suggests, i need to have 2 separate console windows in my application. one should be the "main" console, and the other should be an independent one that has its own inout and output streams. i've read that it's possible to do it using WIN API by creating a new process. i've considered this but i can't seem to get it working:

    Extra consoles - C++ Forum

    can anyone please shed some light for me or help me understand how to do it at all?

    thanks
    Last edited by lmanukyan; 12-14-2017 at 11:13 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    692
    I can't test this idea since I haven't run windows in a while, but perhaps a poor-man's version could work like so:

    1. Start two consoles and determine the pids of the cmd.exe's running in the two consoles by running the following command in each terminal (I've never used this command before, so I don't know if it'll work) :

    wmic process get parentprocessid,name|find "WMIC"

    2. Run your program in one of the consoles, passing in the two pids as command line arguments with the pid belonging to the current console first.

    3. To write to the other console, you detach from the current one and attach to the other one, write something, then switch back.

    The cmd.exe processes will keep the consoles open when you free them (if no process is using a console, it's destroyed). They also provide pids that you can use to (re)attach to the consoles.

    Maybe someone could try this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    #include <windows.h>
    
    DWORD pid_main, pid_other;   // global for convenience
    
    void print_other(const char *fmt, ...) {
        FreeConsole();
        AttachConsole(pid_other);
        va_list va;
        va_start(va, fmt);
        vprintf(fmt, va);
        va_end(va);
        FreeConsole();
        AttachConsole(pid_main);
    }
    
    int main(int argc, char **argv) {
        if (argc != 3) {
            fprintf(stderr, "Usage: dualconsole current_pid other_pid\n");
            exit(EXIT_FAILURE);
        }
    
        pid_main = atoi(argv[1]);
        pid_other = atoi(argv[2]);
    
        printf("starting main\n");
        print_other("starting other\n");
    
        for (int i = 0; i < 10; i += 2) {
            printf("%d\n", i);
            print_other("%d\n", i + 1);
        }
    
        printf("done\n");
    
        return 0;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    thanks for the suggestions.

    it seems logical to me but this code doesn't even display one console. i tried to get an input from the function that creates/deletes the console but it won't even stop there.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I found this, it might help, I have not read it.

    Multiple consoles for a single application - CodeProject

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    692
    Quote Originally Posted by lmanukyan View Post
    it seems logical to me but this code doesn't even display one console. i tried to get an input from the function that creates/deletes the console but it won't even stop there.
    I don't know what you mean by "this code doesn't even display one console". I said that YOU are supposed to open two consoles, determine the pids of the cmd.exe processes running in them, then run your program from one of the consoles, passing the pids as command line parameters (current console first).

    The link userxbw found is actually quite excellent and if I was on windows that's probably what I would use (luckily, on linux it's easy to write to another console).
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    john.c, sorry. i guess it was too early in the morning for me to read carefully.

    i just read it again and it makes sense now and also works and, so thanks!

    but what i'd like to do is not to manually open the consoles but rather have the code to create the consoles. it is possible?

    and btw so how can it be done on linux?

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    .....
    Last edited by userxbw; 12-16-2017 at 12:13 PM.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    692
    Quote Originally Posted by lmanukyan View Post
    i just read it again and it makes sense now and also works and, so thanks!
    but what i'd like to do is not to manually open the consoles but rather have the code to create the consoles. it is possible?
    and btw so how can it be done on linux?
    That is the "poor man's" version, as I said. The other option is to start a "helper" process that opens in its own terminal and communicate with it through a (named?) pipe. That's apparently what the code linked by userxbw does. However, it's C++.

    On linux, if you open two terminals and do ps on one of them you get something like:
    PID TTY TIME CMD
    22657 pts/18 00:00:00 bash
    22804 pts/18 00:00:00 ps

    The terminals are represented as objects in the file system (as is virtually everything on linux), and from the other terminal you can simply write to /dev/pts/18.

    echo hello > /dev/pts/18

    Alternatively you could write to the controlling process's stdout or stderr, so above we would write to the stdout of bash like so:

    echo hello > /proc/22657/fd/1
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert console to windows gui application
    By mrupload in forum C++ Programming
    Replies: 8
    Last Post: 08-07-2014, 07:58 AM
  2. Game loop in Windows console application
    By anon in forum Windows Programming
    Replies: 6
    Last Post: 11-29-2007, 03:26 PM
  3. Windows form application and Windows application
    By |Wiz| in forum Windows Programming
    Replies: 5
    Last Post: 10-01-2005, 04:14 PM
  4. Howto convert a console-application into a windows one?
    By Linuxhippy in forum Windows Programming
    Replies: 2
    Last Post: 06-26-2005, 01:38 AM
  5. Console Application
    By Mont_Blanc in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2004, 03:07 AM

Tags for this Thread