Page 1 of 1

Console lines and progress bar output

Posted: Sun Dec 03, 2023 7:04 pm
by Boulcat
I'm trying to get the console program output with either lines or a progress bar.

The goal is to monitor downloads done with aria2c - - The ultra fast download utility

Code: Select all

D:\Download\aria2c.exe --log-level=notice --log="D:\Download\Aria2c.log" -x16 -s16 --allow-overwrite=true --auto-file-renaming=false -d"D:\Download\Mozilla Firefox Quantum ESR_x64" -o"FirefoxSetup_en_win64.exe" "https://download.mozilla.org/?product=firefox-esr-next-latest-ssl&os=win64&lang=en"
Here for the example with cmd

Code: Select all

Dir /C /O:GS %SystemRoot%
For /L %i IN (1,1,5) Do @(Ping -n 2 127.0.0.1 -w 250 > Nul & Echo | Set /p Dummy=■)
What's wrong in my code for the 1st line Dir /C and ReadProgramData, it crashes if I close the console.
How could it be done if I have both lines and a progress bar?

Code: Select all

EnableExplicit

Procedure.s Oem2Unicode(OEM_in_unicode.s)
  Protected Oem_in_Ascii.s, ByteLength, Unicode.s 
  ByteLength = Len(OEM_in_unicode) + 2      
  Oem_in_Ascii = Space(ByteLength)
  PokeS(@Oem_in_Ascii, OEM_in_unicode, -1, #PB_Ascii)
  Unicode = Space(ByteLength)
  OemToChar_(@Oem_in_Ascii, @Unicode)
  ProcedureReturn Unicode
EndProcedure

Define Size, String.s, Output.s, Error.s

Define CmdParm.s = "/C For /L %i IN (1,1,5) Do @(Ping -n 2 127.0.0.1 -w 250 > Nul & Echo | Set /p Dummy=■)"
;Define CmdParm.s = "/C Dir /C /O:GS %SystemRoot%"
Define hCmd = RunProgram("cmd.exe", CmdParm, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)

If hCmd
  While ProgramRunning(hCmd)
    Size = AvailableProgramOutput(hCmd)
    If Size
      
      ; Looks good with For /L but not with Dir /C, with at the end, if you close the console: The debugged executable quit unexpectedly
      Define *Buffer = AllocateMemory(1024)
      Size = ReadProgramData(hCmd, *Buffer, Size)
      If Size
        String = Oem2Unicode(PeekS(*Buffer, Size, #PB_Ascii))
        Output + String
        ClearDebugOutput()
        Debug Output
      EndIf
      
      ; Looks good for Dir /C. With For /L the progress bar is only displayed at the end 
      ;String = Oem2Unicode(ReadProgramString(hCmd, #PB_Ascii))
      ;Debug String
      ;Output + String + #CRLF$
      
      Error + Oem2Unicode(ReadProgramError(hCmd, #PB_Ascii)) + #CRLF$
    EndIf
  Wend
  If ProgramExitCode(hCmd) <> 0 
    Error + Oem2Unicode(ReadProgramError(hCmd, #PB_Ascii)) + #CRLF$
    MessageRequester("Error " +  GetCurrentDirectory(), "Error Cmd:" + Error, #PB_MessageRequester_Ok | #PB_MessageRequester_Error)
  EndIf
  ;Debug Output
  CloseProgram(hCmd)
EndIf

; IDE Options = PureBasic 6.03 LTS (Windows - x64)
; ExecutableFormat = Console
; EnableXP
; CompileSourceDirectory

Re: Console lines and progress bar output

Posted: Sun Dec 03, 2023 8:11 pm
by fryquez

Code: Select all

Define *Buffer = AllocateMemory(1024)
Size = ReadProgramData(hCmd, *Buffer, Size)
You only allocate 1024 bytes for this *Buffer,
if Size is bigger than that, it will crash.

Re: Console lines and progress bar output

Posted: Sun Dec 03, 2023 10:43 pm
by Boulcat
Thank you, it seems so simple once you know the reason :oops: