How to redirect STDIN of CMD.exe in C++ programme?

How to redirect STDIN of CMD.exe in C++ programme

Hi All
I am trying to redirect STD handles of cmd.exe in my programmes. I am able to redierct STDOUT and STDERR, but rediercting STDIN is not workin. I wiash that whenevr i create a new process "CMD.exe" it should obtain its input from a file say input is 'dir' and write the output to a file.

I am able to write the output to a file , but I am unable to redirect the STDIN of the process. I have tried using CreatePipe , but eve that is not helpin. Any live CODE example on how to do it will be very helpful.

Thanks in advance..
Sumit Chawla



Answer this question

How to redirect STDIN of CMD.exe in C++ programme?

  • Next

    I found several links that you might find helpful through google:
    One from Microsoft (linked to by the other links):
    http://support.microsoft.com/kb/q190351/
    and two from CodeProject:
    http://www.codeproject.com/threads/redir.asp
    http://www.codeproject.com/threads/consolepipe.asp

    Alternately, to launch arbitrary remote command-line processes, you can use the PsExec program found in SysInternal's PsTools package and redirect PsExec's input/output and let it worry about the remote process management:
    http://www.sysinternals.com/Utilities/PsTools.html

    -Extrarius

  • solidtransient

  • Craig Harrison

    No CMD.EXE itself does not use the inpuit from a pipe. If you want to excute some stuff in the CMD.EXE write your commands into a .BAT file and execute it with CMD.EXE. Thats much simpler.

  • whitesky

    Hi

    Thanks for the suggestion. But that does not suit me need. Because that way the CMD.exe cant be made to behave in an intercative way. Infact i want o invoke this functionnality on a remote computer. If i use batch file than it would not work for interactive commands like 'More'  and dir /p...

    Can you suggest any other way of acheving this functionality..

    Thanks ...


  • D. Schwabe

    HI

    Thanks for the answer. yes I have chacked this article. In this Article the Child.exe uses the Readfile function to read from the Pipe. But in my case the Process is CMD.exe. According to standard definition , i create a pipe , and assocated the Read Handle of this pipe to Startup_Info structure while creating the CMD.exe process.

    Then process starts and waits for the Input from Pipe. then i wrote the "dir" command to the other end of pipe using WriteFile function. But the "Cmd.exe" does not read it from the pipe. I have mayself tried to use the ReadFile function to chack if data is getting there to the other end of pipe, and was able to successfully read from the pipe. But the process CMD.exe does not behave the same.

    Kindly guide, or point to some code example where STDIN of CMD.exe have been redirected to a file or Pipe.

    I also tried to redirect STDIN of CMD.exe to a HANDLE to an inuput file, but no gain.ON the other hand , I am able to successfully redierct the STDOUT and STDERR of CMD.exe using HANDLES to files.

    Thanks

  • How to redirect STDIN of CMD.exe in C++ programme?