Input/Output of Console from Win32 App

  • Thread starter computerex
  • Start date
  • Tags
    App
In summary, the program redirects the output of commands to a file, but the commands aren't actually run in one process. This makes commands such as "cd" useless.
  • #1
computerex
68
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
  • #2
bat file?
 
  • #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.
 
  • #4
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.
 
  • #5
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
 

FAQ: Input/Output of Console from Win32 App

What is the purpose of Input/Output in a Win32 application?

The purpose of Input/Output (I/O) in a Win32 application is to allow communication between the application and the user. This includes receiving input from the user, such as keyboard or mouse clicks, and displaying output to the user, such as text or graphics.

How do I receive input from the user in a Win32 application?

To receive input from the user in a Win32 application, you can use functions such as GetKeyboardState or GetCursorPos to retrieve keyboard or mouse input, respectively. These functions can be called within a loop to continuously check for user input.

How do I display output to the user in a Win32 application?

To display output to the user in a Win32 application, you can use the printf function to print text to the console or the TextOut function to display text on a window. You can also use graphics libraries such as Direct2D or GDI to display images or animations.

How can I handle errors or invalid input in a Win32 application?

To handle errors or invalid input in a Win32 application, you can use conditional statements and error handling techniques. For example, you can use if statements to check if the input meets certain criteria and display an error message if it does not. You can also use try/catch blocks to catch and handle any errors that may occur during the execution of the application.

Can I save or load data from a file in a Win32 application?

Yes, you can save or load data from a file in a Win32 application by using file I/O functions such as fopen, fread, fwrite, etc. These functions allow you to open, read, and write to files on the computer's hard drive. You can use this to store user data or settings for the application.

Similar threads

Back
Top