0

I am trying to create an application that will mimic cmd.exe.

My application should be able to launch new processes, print their output, and finish once the child process is done.

This works find with console application, but I have a problem with GUI apps.

My logic is as follows:

  1. In the parent process, create a STDOUT redirection pipe (that will be used by the child process). This pipe will be inherited to the child, and will be used as STDOUT. This is achieved by using CreateProcess + StartupInformation struct (more info here).

  2. Once the child process is created, I use ReadFile to read from the pipe. Once ReadFile fails (or reads 0 bytes), I understand the session has ended.

My problem with GUI apps is that they hang on ReadFile and never end, only if the process has been terminated.

One can say that this is a normal behavour (and I would agree) but this is not how cmd.exe works.

One can open cmd.exe and launch calc.exe, you will notice that cmd does not hang and is straight ahead ready for new input.

It is like cmd.exe "knows" this is a GUI app and their is no need to wait.

My question is how do I mimic cmd.exe, what will be the solution to prevent hanging on GUI apps ?

Thanks alot,

Michael.

3
  • would really appreciate a comment .. :(
    – Michael
    Oct 25, 2014 at 22:17
  • 1
    To determine whether a process has exited, check whether the process handle is signaled. If you want to poll, you can use WaitForSingleObject with a timeout of zero. Redirecting output to a pipe will change the behaviour of many command-line applications, i.e., they will start buffering output, which is usually undesirable and can often be fatal. The output will go to the console by default, that's what cmd.exe does. Oct 26, 2014 at 0:56
  • 1
    I'm not sure how cmd.exe determines whether the child is a console or GUI process. One solution is to read the executable file headers yourself, the format is documented. You could perhaps try launching the process and then using GetConsoleProcessList to see whether the process has become attached to the console or not. (To avoid a race condition, I guess you should launch the process suspended, and resume it only after checking the console process list.) Oct 26, 2014 at 0:59

1 Answer 1

0

Thanks for the replies but they didn't fix the problem.

I found the answer by myself my reversing cmd.exe and its embarrassing simple.

cmd.exe does not redirect IO by changing the child process STD handles, it can be clearly seen in the assembly.

So how is the child process output printed to the screen ?

It seems that if one console application tries to call CreateProcess on another console application, a new console will not be opened. In face the new process will "live" inside the console of the parent process. If one desire 2 consoles to be opened, one should specify the CREATE_NEW_CONSOLE flag.

So how does cmd.exe work ?

It simply calls CreateProcess, with no fancy pipes or stuff like that.

If the child process is a console application its output will be printed to the cmd.exe console (Because only one console exists).

If the child process is not a console application it will be created and cmd.exe will be ready to go on its way.

The following code is all you need:

STARTUPINFO si;
PROCESS_INFORMATION pi;

memset(&pi, 0, sizeof(pi));
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
CreateProcessA(NULL, "ipconfig", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1
  • That's what I said: "The output will go to the console by default, that's what cmd.exe does." You still need to determine whether the child is a console application or not, though, since if it is a console application you need to wait for it to exit. Oct 27, 2014 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.