Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Input/Output of Console from Win32 App

  1. Sep 7, 2008 #1
    Hello. I am writing a simple application which allows you to run shell commands.

    This is the method I used:

    Code (Text):

    void run(std::string cmd, HWND hDlg)
        cmd = cmd + " >> ctoutput.txt";
        LoadFile(GetDlgItem(hDlg, IDC_EDIT_OUTPUT), "ctoutput.txt");
    The user entered command is ran using the system() function, and the output is redirected to a file, the program reads the file and posts the output in the editbox. The problem is, the commands aren't run in one process. It seems system() creates a sub-process, and the process terminates upon running the command. Which makes commands such as, "cd" useless. Any workarounds for this?
  2. jcsd
  3. Sep 7, 2008 #2


    User Avatar

    Staff: Mentor

    bat file?
  4. Sep 7, 2008 #3
    Hmm. I want this program to act like a terminal, I thought it would be a piece of cake to redirect output/input to a console, but apparently it isn't. I have tried allocating a console for the application with the AllocConsole() API method, and then getting the input/output handles using the GetStdHandle() method, then using WriteFile and ReadFile functions to do input/output, but this doesn't seem to work, as the program simply ends up writing commands in the console, no processing happens. For example, sending a "dir" command would simply write "dir" on the console window, and do nothing else.
  5. Sep 18, 2008 #4


    User Avatar

    HI, computerex,
    Though I'm not familiar with " redirect console", but I do find some good stuff.
    there are many articles with source code in www.codeproject.com talking about " redirect console" issue. These may be helpful for you. Good luck.
  6. Sep 18, 2008 #5


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    "cd" is not normally an actual executable program -- it is handled internally by all shells (to my knowledge). It is the shell's responsibility to keep track of the working directory.

    - Warren
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook