Thread: One process with two console windows

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Question One process with two console windows

    I want to have a console process then this process should create another console window. The first process should have access to this new window handles so it can read from and write into it.
    you can create a console window for a process that hasn't have it by AllocConsole() you can also create a console process by calling CreateProcess() and setting CREATE_NEW_CONSOLE. But last one needs a module. It doesn't give us a new window but a new process. But I just need a console window.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One process can only have one console - the only option beyond that is to do your own windows (not console ones, but regular windows) - or have multiple processes that use for example shared memory to access data from a common process, of course.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    or have multiple processes that use for example shared memory to access data from a common process, of course.
    Yeah I should create a console application that gets output from and sends input to another proccess. This way one process can virtualy have multiple console windows by executing that module and somehow (pipe, shared memory, etc) comunicate with that.

    I think a process can't use console handles of another process. Am I right?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Correct, a console is owned by a particular process, and other processes can not use that console (except when the current process creates a new process, the new process becomes the owner until it either relinquishes access to the console, or it exits).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Not quite, you can connect to any process' console (subject to permissions) from XP and above using AttachConsole. You can use it, along with child processes acting as "Console Surrogates" to give the illusion of multiple consoles for a single process without using any IPC. I've almost finished writing such a framework, if anybody is interested I'll post it.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    A process can be attached to at most one console. If the calling process is already attached to a console, the error code returned is ERROR_ACCESS_DENIED (5). If the specified process does not have a console, the error code returned is ERROR_INVALID_HANDLE (6). If the specified process does not exist, the error code returned is ERROR_GEN_FAILURE (31).
    It is from your link. I will be glad to see your framework.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The "illusion", I assume, is achieved by attaching to the console you wish to manipulate. Which means the "surrogates" would have to detach from their consoles. So you attach, back and forth, as needed to manipulate one console at a time.

    May not be ideal, due to potential bottle-neck, if you plan on spawning a thread per-console - which do constant updates. The advantage is that you can use the Win32 console API's directly, instead of using some home-grown API (which uses IPC under the hood).

    gg

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Which means the "surrogates" would have to detach from their consoles.
    That's not right. A console can be attached by many processes. But a process can only attach to one console. So the "surrogates" would be just for creating another console - and keeping the attachment count >= 1.

    gg

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Codeplug View Post
    The "illusion", I assume, is achieved by attaching to the console you wish to manipulate. Which means the "surrogates" would have to detach from their consoles. So you attach, back and forth, as needed to manipulate one console at a time.
    Which means you need 3 processes for having 2 consoles. 2 processes are the "hosts" for the consoles and the third one is the real application which switches between them.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows Process Info
    By P4R4N01D in forum Windows Programming
    Replies: 14
    Last Post: 01-17-2008, 03:56 AM
  2. windows console probs
    By henroid815 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2003, 03:36 AM
  3. multiple console windows
    By gordy in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-13-2002, 11:05 PM
  4. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM
  5. windows dos console
    By dune911 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-16-2002, 11:30 PM