Jump to content
Sign in to follow this  
jwseek

Difficulty with stdinWrite and StdoutRead

Recommended Posts

jwseek

So, basically, DOS programs executed with the "Run" command seem to lock up when either of these commands are called. I'm only experimenting right now, naturally meaning that I'm rather close to the examples I saw, but I'm trying...

$procDiskPart = Run(@ComSpec & " /c DiskPart.exe", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

While StringRight(StdoutRead($procDiskPart, -1, True), 10) <> "DISKPART> "
    Sleep(100)

    If Not(ProcessExists($procDiskPart)) Then
        $iChoice = MsgBox(5 + 64, "Disk Partitioner", "Disk Partitioner could not connect to the Disk Management services." & @CRLF & @CRLF & _
                                "Click Retry to try connecting again or choose Cancel to abort.")
        If $iChoice = 4 Then
            $procDiskPart = Run(@ComSpec" /c DiskPart.exe", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
        Else
            $procDiskPart = 0
            ExitLoop
        EndIf
    EndIf
Wend
        
If $procDiskPart <> 0 Then
    $read = StdoutRead($procDiskPart)
    StdinWrite($procDiskPart, "LIST DISK" & @CRLF)
Else
    MsgBox(0,"","Did not load")
EndIf

$read = StdoutRead($procDiskPart)
MsgBox(0,"",StringLen($read) & " " & $read & @LF & $procDiskPart)

I know it never reaches the MsgBox command at the bottom of the code because I've never seen it. It just seems to sit at the load of the program from the run command...

What am I doing wrong?

Edit: the Run command is not failing (I get a process id with a MsgBox right after it). I suppose the While or something there after is causing the break...

Edit 2: Aha! It was the while that was causing the lock-up. Now I suppose the new question is:

Why doesn't stdoutRead pull anything when I call it? I get a blank string of length 0 every time...

Please help...

Edited by jwseek

Share this post


Link to post
Share on other sites
Squirrely1

I found a sytax error using Ctrl-F5 in SciTe, on this line:

$procDiskPart = Run(@ComSpec" /c DiskPart.exe", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

This survived a syntax check, and looks more proper, on cursory inspection:

#include <Constants.au3>
$procDiskPart = Run(@ComSpec & " /c DiskPart.exe", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

While StringRight(StdoutRead($procDiskPart, -1, True), 10) <> "DISKPART> "
    Sleep(30)

    If Not (ProcessExists($procDiskPart)) Then ExitLoop
WEnd

$iChoice = MsgBox(5 + 64, "Disk Partitioner", _
    "Disk Partitioner could not connect to the Disk Management services." & @CRLF & @CRLF & _
    "Click Retry to try connecting again or choose Cancel to abort.")
If $iChoice = 4 Then
    $procDiskPart = Run(@ComSpec & " /c DiskPart.exe", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
Else
    $procDiskPart = 0
EndIf

If $procDiskPart <> 0 Then
    $read = StdoutRead($procDiskPart)
    StdinWrite($procDiskPart, "LIST DISK" & @CRLF)
Else
    MsgBox(0, "", "Did not load")
EndIf

$read = StdoutRead($procDiskPart)
MsgBox(0, "", StringLen($read) & " " & $read & @LF & $procDiskPart)
Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
jwseek

yeah...the syntax error was the fault of bad copying from SciTE to my browser...Thanks, though.

And perhaps you should close the stream right after you do this ...

StdinWrite($procDiskPart, "LIST DISK" & @CRLF)
I can't close the stream right then because I need to read the output of the stream before I close it (and after trying it anyway, the results remain the same).

I guess I'm still wondering why StdoutRead won't return a value...

Share this post


Link to post
Share on other sites
Squirrely1

I'm not going to run this code, because I don't want to risk my machine; but this looks more like what you might really want:

#include <Constants.au3>
Sleep(300)
Global $procDiskPart, $read

$procDiskPart = Run(@ComSpec & " /c DiskPart.exe", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

While 1
    $read = StdoutRead($procDiskPart); read in normal rather than binary mode.
    If StringRight($read, 10) <> "DISKPART> " Then ExitLoop
    If Not (ProcessExists($procDiskPart)) Then ExitLoop
    Sleep(30)
WEnd

$read = ""

$iChoice = MsgBox(5 + 64, "Disk Partitioner", _
    "Disk Partitioner could not connect to the Disk Management services." & @CRLF & @CRLF & _
    "Click Retry to try connecting again or choose Cancel to abort.")
If $iChoice = 4 Then
    $procDiskPart = Run(@ComSpec & " /c DiskPart.exe", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    $read = StdoutRead($procDiskPart)
    StdinWrite($procDiskPart, "LIST DISK" & @CRLF)
    $read = StdoutRead($procDiskPart)
    MsgBox(0, "", StringLen($read) & " " & $read & @LF & $procDiskPart)
Else
    $procDiskPart = 0
    MsgBox(0, "", "Did not load")
EndIf

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
jwseek

Yeah...thanks. I've been messing around and it looks like my problem is mostly due to timing. If I wait a little while before trying anything fancy (like reading from the command line), I seem to get actual information.

Also, don't worry about the code right now. All it does is list the physical drives on the computer.

Edit: Ooo!

If StringRight($read, 10) <> "DISKPART> " Then ExitLoop

is actually backwards. I want to exit the read loop only when the last ten characters *are* "DISKPART> ", not when they're not.

I guess it should be:

If StringRight($read, 10) == "DISKPART> " Then ExitLoop
Edited by jwseek

Share this post


Link to post
Share on other sites
Squirrely1

Studying other people scripts and re-working them to suit your needs helps a lot - but yeah, you just have to experiment a lot with this language to see what techniques work best.

Still to this day, years later, I will still create several little test files each day, which just test one particular return type from a single standard function. I name them x.au3 usually, and just overwrite them, one after another.

Edited by Squirrely1

Das Häschen benutzt Radar

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.