-2

I do this:

if (!CreateProcessA(NULL, lArgs, NULL, NULL, FALSE,
    CREATE_NEW_CONSOLE, NULL, NULL, &StartupInfo, &ProcessInfo))
{
    free(lArgs);
    return GetLastError();
}

But I don't know how to obtain the input from the console.

Am I oblige to write a file for it ?

EDIT 1: there is no trouble, it works (and the code is both C and C++). this create a console, what I am looking for is: How do I obtain in this scope the user input from this new console ? lArgs contains the parameters/command line I pass to this new console.

EDIT 2: (I don't have any trouble. it works fine.) The child process is a console and I am trying to obtain the user input in the console (child process) to the parent process.

EDIT 3: The child process is just a console as in "CREATE_NEW_CONSOLE". The command line is given to the console with lArgs. This command runs a program of which the result is displayed in this new console. If I use _popen (I don't want to) instead of createprocess this displayed result is returned by _popen, but with createprocess, it just seems to be lost.

user6795571
  • 139
  • 1
  • 7
  • 1
    C and C++ are different languages! Pick the one you use and remove the wrong tag! – too honest for this site Sep 16 '16 at 07:55
  • Shouldn't `lArgs[0]` get the input from that console? – CristiFati Sep 16 '16 at 08:02
  • Which process has trouble? The launching or the launched? – Medinoc Sep 16 '16 at 09:07
  • I think this question already been asked (I am not sure it got an answer though): https://stackoverflow.com/questions/35969730/how-to-read-output-from-cmd-exe-using-createprocess-and-createpipe EDIT: you will actually find the answer here: https://stackoverflow.com/questions/19051769/createprocess-redirect-child-stdout-to-parent-stdout-in-c-windows –  Sep 20 '16 at 13:50

2 Answers2

1

OK; it would seem that the problem here is that you're hopelessly confused about the terminology.

The word "console" as in CREATE_NEW_CONSOLE does not mean what you think it means. No process is a console; a console is something a process has. Specifically, a console is the text-based window that command line applications (including but not limited to the command interpreter) use for input and output. The CREATE_NEW_CONSOLE flag says to the system "create a new console window for this child process instead of using mine".

What you're trying to do, apparently, is to to launch a new command interpreter. You do not need the CREATE_NEW_CONSOLE flag to do that.

(Note that if the program you actually want to run is an executable, you don't need the command interpreter at all, regardless of whether you want the program to have a separate console or not. Any executable, including a command-line executable, can run independently, it does not need to have the command interpreter as a parent. Generally speaking you would launch a new command interpreter as a child process only if you want to run a batch file; for the sake of argument, I'll assume that's what you're doing.)

Also, the phrase "user input" means just that - the input that the user provides to the program. In this context, the stuff a person types on the keyboard. The child process, whether that's the command interpreter or another program, is not the user, and in your case it is generating output rather than accepting input. So what you're wanting is called the child process output, or in this case you could alternatively describe it as the command interpreter output.

So, the question you meant to ask:

I am launching a command interpreter with CreateProcess and I want the output to go to my console rather than to the new console. How can I do this?

And the answer is: this is the default behaviour. Take away the CREATE_NEW_CONSOLE flag, which explicitly tells Windows to do exactly what you don't want it to, and it will behave in the way you want.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
-1

You could use _popen instead. It would return the command line input from the user.