Thread: Write in many command prompts

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    20

    Write in many command prompts

    Hi

    Is there any way to write from the same program in many command prompts ? More specifically I use win XP and MS Visual C++ v6. All we know that with a simple printf or cout we can write to the command prompt of our application. But if we want to have two command prompts running in the same application how can we redirect the print or cout output to write in the first or second command prompt ? I know the existence of system(“cmd”); function ( and also I know that is not a safe command but my application is only for me and not commercial ) . This function it only opens new commands prompts without giving the ability to write into them ? Is there any solution ? Also if there is an answer for the previous questions , how can I pass and execute some commands ( dir ,exit ) in that command prompts ?

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    7

    AllocConsole+WriteConsole+CreateProcess, but why?

    Use the Win32 API function 'AllocConsole' to allocate a new console window. Use the console I/O functions to write to your console windows. 'WriteConsole' is one of them - see its help.
    However, the Win32 API docs say:

    A process can be associated with only one console

    ... so this won't give you multiple console windows owned by the one process at the one time :-( Do you really need this?
    You could have your program starrt multiple instances of cmd.exe, each with their own console window, if you really need this.

    If all you want is to execute DOS commands, you don't need a console window at all - just call CreateProcess() to start a new instance of cmd.exe the system shell, and pass it the command-line arguments that tell it to then execute 'dir' or whatever. I believe you can create console windows without putting sub-processes in them, and I know you can create sub-processes without creating console windows for them.

    Alternatively, write the list of DOS commands that you want to execute to a batch file (a text file with the extension 'bat'), then get cmd.exe to run that batch file, either by using CreateProcess to start cmd.exe with cmdline args that tell it to execute the batch file, or perhaps you could even CreateProcess(myfile.bat) directly (I dunno).

    What are you trying to achieve here? It sounds like you're going about it the wrong way, whatever it is, because your request is 'wierd'.

    There's probably a much easier way to do what you want, but I can't tell you it, because you haven't told me what you really want. Gimme the Bigger Picture.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  2. program to make a floppy write protected
    By shrijeetp in forum C Programming
    Replies: 1
    Last Post: 10-03-2005, 06:00 AM
  3. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 04:48 PM
  4. Function to write string 3 times
    By Giggs in forum C++ Programming
    Replies: 15
    Last Post: 12-24-2002, 04:00 PM
  5. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM