Input/Output of Console from Win32 App

  • Thread starter Thread starter computerex
  • Start date Start date
  • Tags Tags
    App
Click For Summary

Discussion Overview

The discussion revolves around the challenges of executing shell commands from a Win32 application, specifically focusing on the input/output handling of console commands and the limitations of using the system() function for command execution.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a method for running shell commands using the system() function, noting that the output is redirected to a file, but this approach creates a sub-process that terminates, making commands like "cd" ineffective.
  • Another participant suggests using a batch file as a potential solution, though no further details are provided.
  • A participant expresses frustration with trying to allocate a console for the application and using the AllocConsole() and GetStdHandle() APIs, stating that commands are written to the console but not processed as expected.
  • A different participant points out that "cd" is not an executable program but an internal command handled by the shell, emphasizing that the shell manages the working directory.
  • One participant recommends looking for articles on redirecting console output, suggesting that there are resources available that may provide helpful insights.

Areas of Agreement / Disagreement

Participants express various challenges and potential solutions regarding command execution and output handling, but there is no consensus on a definitive solution or approach. Multiple competing views and methods are presented without resolution.

Contextual Notes

The discussion highlights limitations related to the handling of internal shell commands and the behavior of the system() function, as well as the complexities of managing console input/output in a Win32 application.

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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
1
Views
4K
  • · Replies 13 ·
Replies
13
Views
22K
Replies
3
Views
12K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K