Quantcast Input/Output of Console from Win32 App Text - Physics Forums Library

PDA

View Full Version : Input/Output of Console from Win32 App


computerex
Sep7-08, 01:20 PM
Hello. I am writing a simple application which allows you to run shell commands.
http://i37.photobucket.com/albums/e80/computerex/ntbso.png

This is the method I used:


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?

Borek
Sep7-08, 01:58 PM
bat file?

computerex
Sep7-08, 03:49 PM
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.

zyh
Sep18-08, 01:26 AM
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.

chroot
Sep18-08, 01:57 AM
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