Input/Output of Console from Win32 App

  • Thread starter Thread starter computerex
  • Start date Start date
  • Tags Tags
    App
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 4K views
computerex
Messages
68
Reaction score
0
Hello. I am writing a simple application which allows you to run shell commands.
ntbso.png


This is the method I used:

Code:
void run(std::string cmd, HWND hDlg)
{
	cmd = cmd + " >> ctoutput.txt";
	system(cmd.c_str());
	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?
 
Physics news on Phys.org
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.
 
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.
 
computerex said:
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?

"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