Input/Output of Console from Win32 App

  • Thread starter Thread starter computerex
  • Start date Start date
  • Tags Tags
    App
Click For Summary
The discussion centers on creating a simple application that executes shell commands and captures their output. The method employed involves using the system() function to run user-entered commands, redirecting the output to a text file, which is then read and displayed in an edit box. However, a key issue arises because system() creates a sub-process for each command, causing commands like "cd" to fail since they do not persist across processes. Suggestions for workarounds include using batch files, but the user seeks a solution that allows the application to function like a terminal. Attempts to allocate a console and manage input/output using the AllocConsole() and GetStdHandle() APIs have not yielded the desired results, as commands are merely echoed rather than executed. The discussion highlights the challenge of handling internal shell commands and the need for a more effective approach to maintain the working directory and process commands as intended.
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?
 
Technology news on Phys.org
bat file?
 
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
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
5K
Replies
1
Views
4K
Replies
3
Views
11K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
12K
  • · Replies 49 ·
2
Replies
49
Views
11K
  • · Replies 5 ·
Replies
5
Views
2K