Sign in to follow this  
Followers 0
mattw112

stdinwrite

5 posts in this topic

#1 ·  Posted (edited)

I'm having some trouble with the stdinwrite and maybe I don't understand it.

So for example if I wanted to run a command window and run dir c:\temp do a msgbox with the results and then type dir c:\temp\*.txt and then get another msgbox with those results how would I?

Below is a start... but it doesn't work for me.

Oh and I'd like it to be from the same command window. What I'm trying to do eventually is work with a command utility I have called diskpar. Basically I launch this then I want to answer a series of questions it responds back with. I'm pretty close and I'm just trying to simplify my problem to see if that gives me a clue what I'm doing wrong.

Thanks,

Terry

#include <Constants.au3>

$foo = Run(@ComSpec & " /c dir c:\temp", "", @SW_HIDE, $STDIN_CHILD + $STDERR_CHILD + $STDOUT_CHILD)

While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read:", $line)


    $line = StdinWrite($foo, "dir c:\temp\*.txt")
    StdinWrite($foo)
    
    $line2 = StdoutRead($foo)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read2:", $line2)

WEnd
Edited by mattw112

Share this post


Link to post
Share on other sites



You are running ComSpec with /c which tells it to run the command and then exit. This means that future interaction with ComSpec fails as it has exited. Use the /k switch instead to keep ComSpec running.

#include <Constants.au3>

$foo = Run(@ComSpec & " /k dir c:\temp", "", @SW_HIDE, $STDIN_CHILD + $STDERR_CHILD + $STDOUT_CHILD)

While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read:", $line)

    StdinWrite($foo, @CRLF) ;start a newline
    $line = StdinWrite($foo, "dir c:\temp\*.txt" & @CRLF); send a new command
    ;StdinWrite($foo) ? sending the handle/pid is not a good idea
   
    $line2 = StdoutRead($foo)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read2:", $line2)
    StdinWrite($foo, 'exit' & @CRLF); exit ComSpec
WEnd

Share this post


Link to post
Share on other sites

Does anyone have a similar example that is reading an actual application stream?

Any application will do....

Share this post


Link to post
Share on other sites

You are running ComSpec with /c which tells it to run the command and then exit. This means that future interaction with ComSpec fails as it has exited. Use the /k switch instead to keep ComSpec running.

#include <Constants.au3>

$foo = Run(@ComSpec & " /k dir c:\temp", "", @SW_HIDE, $STDIN_CHILD + $STDERR_CHILD + $STDOUT_CHILD)

While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read:", $line)

    StdinWrite($foo, @CRLF) ;start a newline
    $line = StdinWrite($foo, "dir c:\temp\*.txt" & @CRLF); send a new command
    ;StdinWrite($foo) ? sending the handle/pid is not a good idea
   
    $line2 = StdoutRead($foo)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read2:", $line2)
    StdinWrite($foo, 'exit' & @CRLF); exit ComSpec
WEnd
Thanks that seems to work and I learned a little about what is going on.

Only question I have is I read someplace that you needed to do the stdinwrite($foo) to close it or something.

Thanks,

Terry

Share this post


Link to post
Share on other sites

Only question I have is I read someplace that you needed to do the stdinwrite($foo) to close it or something.

Ah yes, you are correct. I see in help that is the case. Thanks for informing me.

The optional second parameter is the string that you wish StdinWrite to write to the stream. If the function is called with no second argument, StdinWrite closes the stream and invalidates it for further writing.

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  
Followers 0