Jump to content
Sign in to follow this  
wraithdu

Cmd.exe With Redirected STDIN

Recommended Posts

wraithdu

It's highly possible I'm misunderstanding what's going on here, but why in this quick test is cmd's output ending up in the Scite window? I'm redirecting STDIN, which should just give me access to insert data into cmd's input buffer via StdinWrite(). This is giving similar results to STDERR_CHILD.

$pid = Run("cmd.exe", "", @SW_SHOW, 1) ; stdin_child
ProcessWaitClose($pid)

Share this post


Link to post
Share on other sites
Valik

If AutoIt has a standard handle then it re-uses that handle for the child process. SciTE provides these handles so AutoIt just passes them along.

Share this post


Link to post
Share on other sites
wraithdu

So AutoIt is passing along Scite's STDOUT/ERR handles in this case, which is why I see it in Scite's window? If so, why would it do that when only STDIN_CHILD was requested?

A similar thing seems to be going on if you compile the example. The cmd window is blank. If you compile as CUI then you see cmd's OUT/ERR output in AutoIt's console window.

Edited by wraithdu

Share this post


Link to post
Share on other sites
Valik

I'll take a look but I think handle redirection requires you specify all the handles or none of them. If you specify STDIN then AutoIt must also provide something for STDOUT and STDERR as well.

Share this post


Link to post
Share on other sites
wraithdu

That would make sense to the results I'm seeing. If so, the documentation for Run() should be updated to reflect that fact. Thanks for looking!

Edited by wraithdu

Share this post


Link to post
Share on other sites
Valik

As I thought, the STARTUPINFO structure takes only a single flag to tell it to use STD handles. IIRC the logic is:

  • Is the user specifying this specific stream? If yes, use it, if no...
  • Does AutoIt have a non-null stream? If yes, use it, if no...
  • Get a handle to the NUL stream? If yes, use it, if no...
  • Trouble.
The behavior changes if the $STDIO_INHERIT_PARENT flag is used but that's not really relevant for you.

Edit: I'm not going to document this behavior, however.

Edited by Valik

Share this post


Link to post
Share on other sites
wraithdu

So then hStdInput/Output/Error members of the structure are always the same if any of STDOUT_CHILD / STDERR_CHILD / STDIN_CHILD flags are specified? That doesn't seem to be true when testing my example with STDOUT_CHILD or STDERR_CHILD. In those cases, the cmd window quickly runs and exits, leaving me with data to be read via StdoutRead/StderrRead, while STDIN_CHILD causes the cmd window to remain.

If any handle is used then the flag is set and all three handle members must be specified, but surely the defaults (keyboard buffer for STDIN, console window buffer for OUT/ERR) can be used depending on the user's request in the Run() function?

Edited by wraithdu

Share this post


Link to post
Share on other sites
wraithdu

I didn't, was asking if that was the case. Bad phrasing on my part.

The second part of my question is still valid though. Is it possible to fall back on the defaults instead of one of AutoIt's handles?

Share this post


Link to post
Share on other sites
Valik

So then hStdInput/Output/Error members of the structure are always the same if any of STDOUT_CHILD / STDERR_CHILD / STDIN_CHILD flags are specified?

I never said they were the same. What gives you the impression that I did?

That doesn't seem to be true when testing my example with STDOUT_CHILD or STDERR_CHILD. In those cases, the cmd window quickly runs and exits, leaving me with data to be read via StdoutRead/StderrRead, while STDIN_CHILD causes the cmd window to remain.

When the SciTE STDIN handle is used, SciTE controls it. When you specify your own STDIN handle, YOU control it thus YOU are responsible for closing it to signal to the application that there's no more input and it can close.

If any handle is used then the flag is set and all three handle members must be specified, but surely the defaults (keyboard buffer for STDIN, console window buffer for OUT/ERR) can be used depending on the user's request in the Run() function?

Pray do tell how the parent is supposed to get the child's STDIO handles when the child doesn't exist yet.

Please stop over-complicating things. The simple answer is this: If you want explicit control, explicitly specify all the handles. It's really that simple. Allowing things to use their default behavior results in idiosyncrasies like this. The other simple thing is this: Never run a script that uses STDIO redirection from within SciTE or you will get very strange behavior.

Share this post


Link to post
Share on other sites
wraithdu

I understand now, thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×