Jewtus

stdoutread not picking up error

10 posts in this topic

I'm trying to run a command line to create a network shared folder and make sure that I don't get an access denied error. When I use the following:

$pid = RunWait('"' & @ComSpec & '" /c net share SharedFolder="C:\TEST" /grant:everyone,FULL', '', @SW_HIDE, $STDOUT_CHILD)
    $sOutput = StdoutRead($pid)
    MsgBox(0, "", $sOutput)

It runs but a blank msgbox shows up and the following is written to the SCITE console:

System error 5 has occurred.

Access is denied.

Why isn't this being captured? Is there any way of telling if the command executed correctly?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try this:
 

#RequireAdmin

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

_Example()
Func _Example()
    Local $iPID = RunWait('"' & @ComSpec & '" /c net share SharedFolder="C:\TEST" /grant:everyone,FULL', '', @SW_HIDE, $STDOUT_CHILD)
    Local $sOutput = ''
    
    While 1
        $sOutput &= StdoutRead($iPID)
        ; Exit the loop if the process closes or StdoutRead returns an error.
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)
    
EndFunc

 

Edited by mLipok
script fix :)

NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-11-12
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

@mLipok: there is also a handle to $STDERR_CHILD needed.

#RequireAdmin
_Example()
Func _Example()
    Local $pid = RunWait('"' & @ComSpec & '" /c net share SharedFolder="C:\TEST" /grant:everyone,FULL', '', @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)

    While 1
        $sOutput = StdoutRead($iPID)
        ; Exit the loop if the process closes or StdoutRead returns an error.
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)
    
EndFunc

 

Edited by AutoBert

Share this post


Link to post
Share on other sites

look in my edit:  script fix :)


NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-11-12
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

I tried to run both examples, but I still get a blank msgbox. Its weird because I use the exact same function elsewhere and I get the readout, but it doesn't capture this error.

 

I actually really only care to capture the failures (if it is successful I just want the script to continue).

Share this post


Link to post
Share on other sites

:oops:


NEW:     ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-11-12
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

I assume it should have been Run instead of RunWait?

I tried just run and still I get the empty msgbox. I thought that maybe the stdoutread was getting wiped out when it errors so I even tried using a temp string but still no luck:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

_Example()
Func _Example()
    Local $pid = Run('"' & @ComSpec & '" /c net share SharedFolder="C:\TEST" /grant:everyone,FULL', '', @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
    local $sOutput
    While 1
        $sTemp = StdoutRead($pid)
        ; Exit the loop if the process closes or StdoutRead returns an error.
        If @error Then
            ExitLoop
        Else
            $sOutput=$sTemp
        EndIf
        Sleep(10)
    WEnd
    MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)

EndFunc

 

Share this post


Link to post
Share on other sites

There is a reason why we have these nice examples in the helpfile. ;)
You aren't reading the STDERR so don't get the error: Quick and dirty fix:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

_Example()
Func _Example()
    Local $pid = Run('"' & @ComSpec & '" /c net share SharedFolder="C:\TEST" /grant:everyone,FULL', '', @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
    local $sOutput
    While 1
        $sTemp = StdoutRead($pid)
        $sTemp &= StderrRead($pid)
        ; Exit the loop if the process closes or StdoutRead returns an error.
        If @error Then
            ExitLoop
        Else
            $sOutput&=$sTemp
        EndIf
        Sleep(10)
    WEnd
    MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)

EndFunc

 

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#10 ·  Posted

Ohhhhhh that makes sense. I didn't realize they worked separately. That makes WAY more sense. Thanks @Jos!

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