Can not to read program console output :(

Just starting out? Need help? Post your questions and find answers here.
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Can not to read program console output :(

Post by dibor »

Hello gang.
Have troubles while reading program console output.
When I run program in the command prompt I got this output:

Image

If I run code like this

Code: Select all

            Program = RunProgram ("sedutil-cli.exe", " --scan", "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
            Debug "Program is " + Program
           
            
            String$ = ""
              If Program 
                  While ProgramRunning(Program)
                      If AvailableProgramOutput(Program)
                          String$ = String$ + ReadProgramString(Program) + Chr(13)
                      EndIf
                  Wend
                  String$ + "Exitcode: " + Str(ProgramExitCode(Program))
                  CloseProgram(Program) ; Close the connection to the program
                  
                    Debug "Out is : " + String$
I see in the debug window only:
Image

Two middle lines I do not get :(

What I do wrong ?

Thank you.
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Can not to read program console output :(

Post by User_Russian »

Program = RunProgram ("sedutil-cli.exe", "--scan", "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Can not to read program console output :(

Post by dibor »

What do U mean?
This is same command what I wrote.
"--scan" without space, this is doesn't matter.
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Can not to read program console output :(

Post by infratec »

Maybe it writes to stderr instead of stdout.
So try to read the error output too.
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Can not to read program console output :(

Post by ChrisR »

It seems that you have not activated the administrator mode in the compilations options
With your code:

Code: Select all

Program is 36244608
Out is : 
Scanning for Opal compliant disks
\\.\PhysicalDrive0 No       
\\.\PhysicalDrive1 No       
No more disks present ending scan
Exitcode: 0
Gérard
User
User
Posts: 43
Joined: Sat Oct 17, 2015 6:00 pm
Location: France
Contact:

Re: Can not to read program console output :(

Post by Gérard »

Hello,
On my PC with Administrator mode and your code

Code: Select all

Program is 36900000
Out is : 
Scanning for Opal compliant disks
\\.\PhysicalDrive0  2  Samsung SSD 870 EVO 1TB                  SVT02B6Q
No more disks present ending scan
Exitcode: 0
You can try

Code: Select all

ClearClipboard()
Program = RunProgram ("cmd.exe","/c sedutil-cli.exe -v --scan 2>&1 | clip", "", #PB_Program_Wait)
Debug GetClipboardText()
■ Win10 64-bit (Intel Celeron CPU N2920 @ 1.86GHz, 4,0GB RAM, Intel HD Graphics) & PB 6.00 LTS
■ Vivre et laisser vivre.
■ PureBasic pour le fun
■ cage sur le forum Français
■ Mes sites: http://pbcage.free.fr - http://yh.toolbox.free.fr
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Can not to read program console output :(

Post by dibor »

ChrisR wrote: Mon Jun 27, 2022 5:39 pm It seems that you have not activated the administrator mode in the compilations options
Hi.
Yes, I didn't activate it :(
But when I run console program without admin privilege in the command prompt - I got message about admin privilege.
Because that I forget about admin mode in the compiler :oops:
Will try tomorrow.
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Can not to read program console output :(

Post by dibor »

Gérard wrote: Mon Jun 27, 2022 5:55 pm Hello,
On my PC with Administrator mode and your code......
Thank you too.
I am a NOOB

BTW, U have encrypted drive.

Thanks to all of U
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Can not to read program console output :(

Post by dibor »

Hello guys.

Please explain why this doesn't works.
As in previous example, I have change command parameter to wrong one, I do not get any output message
I write --sca instead --scan

Code: Select all

            Program = RunProgram ("sedutil-cli.exe", " --sca", "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
            Debug "Program is " + Program
           
            
            String$ = ""
              If Program 
                  While ProgramRunning(Program)
                      If AvailableProgramOutput(Program)
                          String$ = String$ + ReadProgramString(Program) + Chr(13)
                      EndIf
                  Wend
                  String$ + "Exitcode: " + Str(ProgramExitCode(Program))
                  CloseProgram(Program) ; Close the connection to the program
                  
                    Debug "Out is : " + String$
		Endif
With #PB_Program_Error flag also I do not get any output

But if RunProgram looks like

Code: Select all

Program = RunProgram("cmd.exe", "/c sedutil-cli.exe --sca 2>&1", "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
I got message : Out is : Invalid command line argument --sca
Gérard answer helped me, but I do not understand what "2>&1" meaning :(

Thank you!
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Can not to read program console output :(

Post by infratec »

There are predifined file handles:

0 stdin
1 stdout
2 stderr

> means redirection
& means the following is a handle and not a filename

So...
redirect all stderr to stdout.
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Can not to read program console output :(

Post by dibor »

Ok.
So if 2>&1 is redirect form stderr why doesnt work without redirect?

Code: Select all

Program = RunProgram ("sedutil-cli.exe", " --sca", "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Error)
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Can not to read program console output :(

Post by ChrisR »

infratec answered before for 2>&1

Direct call, without cmd /c

Code: Select all

Program = RunProgram ("E:\Temp\sedutil\sedutil-cli.exe", "--scan", "", #PB_Program_Hide|#PB_Program_Open|#PB_Program_Read|#PB_Program_Error)
Error$ = ""
Output$ = ""
If Program
  While ProgramRunning(Program)
    If AvailableProgramOutput(Program)
      Output$ + ReadProgramString(Program) + #CRLF$
      Error$ + ReadProgramError(Program) + #CRLF$
    EndIf
  Wend
  If ProgramExitCode(Program) <> 0
    Error$ + ReadProgramError(Program) + #CRLF$
    Debug "Error: " + Error$
  Else
    Debug "Output: " + Output$
  EndIf   
  CloseProgram(Program) 
EndIf
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Can not to read program console output :(

Post by dibor »

Thank you Chris!

In other words I need to read ReadProgramError if ProgramExitCode is exists :)

Best Wishes.
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Can not to read program console output :(

Post by infratec »

In other words:

you did not read what I wrote:

https://www.purebasic.fr/english/viewto ... 71#p586071
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: Can not to read program console output :(

Post by dibor »

Hi
Yes I read it, thank you!
I didn't knew that for reading stderr I need to use ReadProgramError function.
I thought #PB_Program_Error flag will be enough, from the help - #PB_Program_Error : Read the error output of the program. (stderr)
I am a NOOB and do not understand many things.

I can not understand how I can to read few lines of stderr
ReadProgramError reads only one line.
AvailableProgramOutput is only for stdout, so how I can check if additional error lines is available?

With cmd /c and stderr to stdout redirection I can to read 4 lines of error, but with direct call I can't. :(
Last edited by dibor on Thu Jun 30, 2022 8:31 am, edited 1 time in total.
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
Post Reply