Jump to content

Pipe to @ComSpec


KaFu
 Share

Recommended Posts

Just a small (but imho useful) example...

#include <Constants.au3>
; Example 1
$foo = Run(@ComSpec, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
StdinWrite($foo, "dir /b c:" & @CRLF)
StdinWrite($foo, "echo. Echo finished" & @CRLF)
StdinWrite($foo, "exit" & @CRLF); send exit command to trigger stdout
$line = ""
While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend
MsgBox(0,"","Finished" & @crlf & @crlf & @crlf & $line)


; Example 2
; Command1 && Command2; Run second command if first command executed successfully
; Command1 || Command2; Run second command if first command executed unsuccessfully

$foo = Run(@ComSpec & " /k dir /b c: && Echo. && Echo finished && Exit", 'c:\', @SW_HIDE, $STDOUT_CHILD)

$line = ""
While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend
MsgBox(0,"","Finished" & @crlf & @crlf & @crlf & $line)

Best Regards

Edited by KaFu
Link to comment
Share on other sites

  • 5 months later...
Link to comment
Share on other sites

  • 2 months later...

Just a small (but imho useful) example...

#include <Constants.au3>
; Example 1
$foo = Run(@ComSpec, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
StdinWrite($foo, "dir /b c:" & @CRLF)
StdinWrite($foo, "echo. Echo finished" & @CRLF)
StdinWrite($foo, "exit" & @CRLF); send exit command to trigger stdout
$line = ""
While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend
MsgBox(0,"","Finished" & @crlf & @crlf & @crlf & $line)


; Example 2
; Command1 && Command2; Run second command if first command executed successfully
; Command1 || Command2; Run second command if first command executed unsuccessfully

$foo = Run(@ComSpec & " /k dir /b c: && Echo. && Echo finished && Exit", 'c:\', @SW_HIDE, $STDOUT_CHILD)

$line = ""
While 1
    $line &= StdoutRead($foo)
    If @error Then ExitLoop
Wend
MsgBox(0,"","Finished" & @crlf & @crlf & @crlf & $line)

Best Regards

Thanks for the code. I managed to modify

it to drive some command line programs.

However, nothing could be read from a

command line program's STDOUT, which was

written in C++ and compiled with MS VC++

(someone else's program). In order to see

where the problem could be, I wrote a simple

command line program in C and compiled

with LCC - I managed to read data from its

STDOUT. I couldn't work out where the

problem is. Don't cout and printf go to

STDOUT? Is there anything else I can try?

/Why Tea

Link to comment
Share on other sites

Thanks for the code. I managed to modify

it to drive some command line programs.

However, nothing could be read from a

command line program's STDOUT, which was

written in C++ and compiled with MS VC++

(someone else's program). In order to see

where the problem could be, I wrote a simple

command line program in C and compiled

with LCC - I managed to read data from its

STDOUT. I couldn't work out where the

problem is. Don't cout and printf go to

STDOUT? Is there anything else I can try?

/Why Tea

have you tried redirecting STDERR to STDOUT ?

whim

Link to comment
Share on other sites

Hmmm, sorry, don't know about C. Try if it works from command-shell cmd.exe manually. If it pipe to cmd.exe I don't see why it shouldn't be captured. Otherwise maybe refer to the developer chat to ask C questions :D.

Link to comment
Share on other sites

Hmmm, sorry, don't know about C. Try if it works from command-shell cmd.exe manually. If it pipe to cmd.exe I don't see why it shouldn't be captured. Otherwise maybe refer to the developer chat to ask C questions :D.

I think it most likely has nothing to do with

C or C++. The following code works:

#include <Constants.au3>

Local $my_prog = "c:\somewhere\bin\my_prog.exe"

Local $pid = Run(@ComSpec & " /k " & $my_prog)

Sleep(100)

Send ("C")

Sleep(50)

Send ("MS")

Sleep(100)

Send ("0{ENTER}1{ENTER}1{ENTER}2{Enter}")

But I when I tried to hook up the STDIO and

use the StdoutRead()/StdinWrite() so that I

can check the output strings, outputs to

the screen didn't show up anywhere.

I'm very new to Autoit, I basically used your

code as samples to my code. The following code

failed:

Local $pid = Run(@ComSpec, "c:\somewhere\bin\", @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)

StdinWrite ($pid, "my_prog.exe" & @CRLF)

Sleep(150)

StdinWrite ($pid, "C") ; no CR needed

Sleep(50)

StdinWrite ($pid, "M") ; no CR needed

Sleep(50)

StdinWrite ($pid, "S") ; no CR needed

Sleep(50)

StdinWrite ($pid, "0" & @CRLF)

StdinWrite ($pid, "1" & @CRLF)

StdinWrite ($pid, "1" & @CRLF)

StdinWrite ($pid, "2" & @CRLF)

Local $cnt = 1

Local $data = ""

While True

$data &= StdoutRead ($pid)

ConsoleWrite($data)

If @error Then ExitLoop

If StringInStr ($data, "<009>") <> 0 Then

$result = 1

MsgBox (0,"Test Passed!", $data)

ExitLoop

EndIf

Sleep(30)

$cnt = $cnt + 1

If $cnt == 100 Then

ExitLoop

EndIf

WEnd

If $result == 0 Then

MsgBox (0,"Test Failed!", $data)

EndIf

Do you see any fault in the code above?

/Why Tea

Link to comment
Share on other sites

As far as I see the problem is with triggering the output to the console... in my example above the line

StdinWrite($foo, "exit" & @CRLF); send exit command to @ComSpec to trigger stdout!

does this. But I guess you also need a command for your app to close first (to make ComSpec accept "exit")? Try something like

StdinWrite($pid, "Q" & @CRLF) ; send some quit command to your app to exit back to console!

or... even processclose($pid) might work :D...but I'm not sure about that.

#include <Constants.au3>

Local $pid = Run(@ComSpec, "c:\", @SW_SHOW, $STDIN_CHILD + $STDERR_MERGED)
StdinWrite ($pid, "my_prog.exe" & @CRLF)

Sleep(150)
StdinWrite($pid, "C") ; no CR needed
Sleep(50)
StdinWrite($pid, "M") ; no CR needed
Sleep(50)
StdinWrite($pid, "S") ; no CR needed
Sleep(50)
StdinWrite($pid, "0" & @CRLF)
StdinWrite($pid, "1" & @CRLF)
StdinWrite($pid, "1" & @CRLF)
StdinWrite($pid, "2" & @CRLF)

StdinWrite($pid, "Q" & @CRLF) ; send some quit command to your app to exit back to console!

StdinWrite($foo, "exit" & @CRLF); send exit command to @ComSpec to trigger stdout!

While 1
    $data &= StdoutRead($pid)
    If @error Then ExitLoop
Wend

ConsoleWrite($data)

If StringInStr($data, "<009>") <> 0 Then
        MsgBox(0, "Test Passed!", $data)
Else
    MsgBox(0, "Test Failed!", $data)
EndIf
Link to comment
Share on other sites

I think it most likely has nothing to do with

C or C++. The following code works:

#include <Constants.au3>

Local $my_prog = "c:\somewhere\bin\my_prog.exe"

Local $pid = Run(@ComSpec & " /k " & $my_prog)

Sleep(100)

Send ("C")

Sleep(50)

Send ("MS")

Sleep(100)

Send ("0{ENTER}1{ENTER}1{ENTER}2{Enter}")

But I when I tried to hook up the STDIO and

use the StdoutRead()/StdinWrite() so that I

can check the output strings, outputs to

the screen didn't show up anywhere.

I'm very new to Autoit, I basically used your

code as samples to my code. The following code

failed:

Do you see any fault in the code above?

/Why Tea

WhyTea, I understand that you want to get your problem solved, but so do a lot of other users.

It is considered rude to post in someone elses thread asking for help (especially in Example Scripts, where support do not belong at all). Another thing that is considered rude is double posting: http://www.autoitscript.com/forum/index.php?showtopic=101683

Please keep this in mind for next time. You are forgiven already.

Link to comment
Share on other sites

WhyTea, I understand that you want to get your problem solved, but so do a lot of other users.

It is considered rude to post in someone elses thread asking for help (especially in Example Scripts, where support do not belong at all). Another thing that is considered rude is double posting: http://www.autoitscript.com/forum/index.php?showtopic=101683

Please keep this in mind for next time. You are forgiven already.

Thanks for the gentle nudge. It won't happen again. It's just a desperate new user try to get an answer to a seemingly basic problem :D

Link to comment
Share on other sites

As far as I see the problem is with triggering the output to the console... in my example above the line

StdinWrite($foo, "exit" & @CRLF); send exit command to @ComSpec to trigger stdout!

does this. But I guess you also need a command for your app to close first (to make ComSpec accept "exit")? Try something like

StdinWrite($pid, "Q" & @CRLF) ; send some quit command to your app to exit back to console!

or... even processclose($pid) might work :D...but I'm not sure about that.

#include <Constants.au3>

Local $pid = Run(@ComSpec, "c:\", @SW_SHOW, $STDIN_CHILD + $STDERR_MERGED)
StdinWrite ($pid, "my_prog.exe" & @CRLF)

Sleep(150)
StdinWrite($pid, "C") ; no CR needed
Sleep(50)
StdinWrite($pid, "M") ; no CR needed
Sleep(50)
StdinWrite($pid, "S") ; no CR needed
Sleep(50)
StdinWrite($pid, "0" & @CRLF)
StdinWrite($pid, "1" & @CRLF)
StdinWrite($pid, "1" & @CRLF)
StdinWrite($pid, "2" & @CRLF)

StdinWrite($pid, "Q" & @CRLF) ; send some quit command to your app to exit back to console!

StdinWrite($foo, "exit" & @CRLF); send exit command to @ComSpec to trigger stdout!

While 1
    $data &= StdoutRead($pid)
    If @error Then ExitLoop
Wend

ConsoleWrite($data)

If StringInStr($data, "<009>") <> 0 Then
        MsgBox(0, "Test Passed!", $data)
Else
    MsgBox(0, "Test Failed!", $data)
EndIf

Thanks for the reply. I did all those before my last post. Nothing worked. I will get out of here as I shouldn't have budge in in the first place. Sorry about that!
Link to comment
Share on other sites

  • 3 months later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...