Write in many command prompts
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 ?
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.