Page 1 of 1

ReadProgramData() not reading all output for large output

Posted: Sun Jan 21, 2024 8:22 am
by danny88
Hi

I'm trying to make a Windows shell replication where I open CMD with RunProgram(), then i can type commands in another console and get the output live.
The problem is that with large outputs (for instance running 'dir' command on a folder containing 10.000 files) the output from ReadProgramData() is truncated.

Here is my code :

Code: Select all

prog = RunProgram("cmd.exe", "", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Write | #PB_Program_Error | #PB_Program_Hide)
OpenConsole("test")

While ProgramRunning(prog)
  output$ = ""
 
  cmd$ =Input()	; getting command from user
  WriteProgramStringN(prog, cmd$)
  
  Repeat	; if I ommit this waiting loop, the program stucks
    Delay(1)
  Until AvailableProgramOutput(prog)
  
  While AvailableProgramOutput(prog)
    length = AvailableProgramOutput(x)
    If length > 0
      buffer = AllocateMemory(length)
      ReadProgramData(prog, buffer, length)
      output$ + PeekS(buffer, length, #PB_UTF8 | #PB_ByteLength)
      FreeMemory(buffer)
    EndIf
  Wend
  Debug output$
Wend


Where am I wrong ?
Thanks

Re: ReadProgramData() not reading all output for large output

Posted: Sun Jan 21, 2024 10:22 am
by infratec
You block the read with your Input()
Normally you would need an own thread to read the output.

I do now an unblocking input.
Please test it.

Code: Select all

EnableExplicit

Define prog.i, exit.i, Key$, cmd$, length.i, *buffer, output$

prog = RunProgram("cmd.exe", "", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Write | #PB_Program_Error | #PB_Program_Hide)
If IsProgram(prog)
  
  OpenConsole("test")
  Print("Enter 'exit' to terminat the session" + #CRLF$ + ":")
  
  While ProgramRunning(prog) And Not Exit
    
    Key$ = Inkey()
    If Key$ <> ""
      Print(Key$)
      cmd$ + Key$
      If Key$ = #CR$
        Print(#LF$ + ":")
        Debug "CMD: " + cmd$
        If cmd$ <> "exit"
          WriteProgramStringN(prog, cmd$)
          cmd$ = ""
          output$ = ""
        Else
          Exit = #True
        EndIf
      EndIf
    EndIf
    
    length = AvailableProgramOutput(prog)
    If length > 0
      *buffer = AllocateMemory(length)
      If *buffer
        ReadProgramData(prog, *buffer, length)
        output$ + PeekS(*buffer, length, #PB_UTF8 | #PB_ByteLength)
        Debug output$
        FreeMemory(*buffer)
      EndIf
    Else
      Delay(10) ; to avoid 100% cpu load
    EndIf
    
  Wend
  
  CloseProgram(prog)
  
  CloseConsole()
  
EndIf

Re: ReadProgramData() not reading all output for large output

Posted: Sun Jan 21, 2024 11:53 am
by juergenkulow
PB v6.03 LTS: Debugger Output Bug (All OS)

Code: Select all

; Debug max len 16000 Charaters
prog = RunProgram("cmd.exe", "", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Write | #PB_Program_Error | #PB_Program_Hide)
OpenConsole("test")
While ProgramRunning(prog)
  output$ = ""
  cmd$ =Input()	; getting command from user
  If ProgramRunning(prog)
    WriteProgramStringN(prog, cmd$)
    Delay(16) 
    While AvailableProgramOutput(prog)
      length=AvailableProgramOutput(prog)
      Debug length
      buffer = AllocateMemory(length)
      ReadProgramData(prog, buffer, length)
      output$ + PeekS(buffer, length, #PB_UTF8 | #PB_ByteLength)
      Debug Len(output$)
      Debug PeekS(buffer, length, #PB_UTF8 | #PB_ByteLength)
      FreeMemory(buffer)
      Delay(16)
    Wend 
    If output$<>""
      Debug "Len:"+Str(Len(Output$))+":"+output$
    EndIf   
  EndIf 
Wend

Re: ReadProgramData() not reading all output for large output

Posted: Sun Jan 21, 2024 7:24 pm
by danny88
juergenkulow wrote: Sun Jan 21, 2024 11:53 am PB v6.03 LTS: Debugger Output Bug (All OS)

Code: Select all

; Debug max len 16000 Charaters
prog = RunProgram("cmd.exe", "", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Write | #PB_Program_Error | #PB_Program_Hide)
OpenConsole("test")
While ProgramRunning(prog)
  output$ = ""
  cmd$ =Input()	; getting command from user
  If ProgramRunning(prog)
    WriteProgramStringN(prog, cmd$)
    Delay(16) 
    While AvailableProgramOutput(prog)
      length=AvailableProgramOutput(prog)
      Debug length
      buffer = AllocateMemory(length)
      ReadProgramData(prog, buffer, length)
      output$ + PeekS(buffer, length, #PB_UTF8 | #PB_ByteLength)
      Debug Len(output$)
      Debug PeekS(buffer, length, #PB_UTF8 | #PB_ByteLength)
      FreeMemory(buffer)
      Delay(16)
    Wend 
    If output$<>""
      Debug "Len:"+Str(Len(Output$))+":"+output$
    EndIf   
  EndIf 
Wend
Work perfectly !
Thanks a lot !!!!