Page 1 of 1

RunProgram with #PB_Program_Hide not working as expected

Posted: Wed Aug 16, 2023 6:31 pm
by dave@idea-tech.com
Hi, I am writing a PB program to run and control WinSCP.

WinSCP.com will open WinSCP in console mode.

Here's my code:

Code: Select all

OpenConsole( "Control WinSCP",#PB_Ascii)

Delay(4000)

winscp_handle = RunProgram( "C:/Program Files (x86)/winscp/winscp.com ","","",#PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Ascii|#PB_Program_Hide  , 0  )
The Console "Control WinSCP" opens but 4 seconds later, the console title changes to "WinSCP" .

I was expecting to have full control of the "Control WinSCP" console and communicate with the hidden WinSCP program with WriteProgramString and ReadProgramString

What am I not understanding about consoles? Thanks!

Re: RunProgram with #PB_Program_Hide not working as expected

Posted: Wed Aug 16, 2023 10:59 pm
by Gérard
Hello,
I tried to write few lines of code to understand the question
I don't see why Delay(4000) in your code.
You also need to prevent the console from closing.
Here is my test code

Code: Select all

EnableExplicit

Define console, winscp_handle, Event, string$

console=OpenConsole("Control WinSCP",#PB_Ascii)

#flags=#PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Ascii|#PB_Program_Hide

winscp_handle = RunProgram("winscp.com","","",#flags,console)

If console And winscp_handle
  If OpenWindow(0,0,0,100,100,#Empty$,#PB_Window_Invisible)
    PrintN("Winscp: help to displays help.")
    PrintN(#Empty$)
    WriteProgramStringN(winscp_handle,#Empty$,#PB_Ascii)
    Repeat
      Event=WaitWindowEvent(20)
      string$=Input()
      If string$<>#Empty$
        Select string$
          Case "exit" : Break
          Default
            WriteProgramStringN(winscp_handle,string$+#CRLF$,#PB_Ascii)
            While AvailableProgramOutput(winscp_handle)
              PrintN(ReadProgramString(winscp_handle,#PB_Ascii))
            Wend
        EndSelect
      EndIf
    ForEver
  EndIf
  CloseConsole()
EndIf
Help Remarks about ReadProgramString(winscp_handle,#PB_Ascii)

However, the function remains blocked indefinitely if the program requests the intervention of the user. For example, return a yes / no choice to the program to continue.
Gérard

Re: RunProgram with #PB_Program_Hide not working as expected

Posted: Thu Aug 17, 2023 4:40 pm
by Gérard
Hi,
Here is an example of controlling WinScp via console that works correctly:

Code: Select all

EnableExplicit

Global console, winscp_handle, Event, string$, N

console=OpenConsole("WinSCP Console",#PB_Ascii)

#flags=#PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Ascii|#PB_Program_Hide

;;; WinSCP.com and WinScp.exe are in the %PATH%
winscp_handle = RunProgram("winscp.com","","",#flags,console)

Structure Command
  Command$
EndStructure

Global *Parameters.Command, Thread

Procedure sendCommand(*Parameters.Command)
  Protected string$
  string$=*Parameters\Command$
  Debug "string="+string$
  WriteProgramStringN(winscp_handle,#CRLF$)
  WriteProgramStringN(winscp_handle,string$+#CRLF$)
  Delay(50)
  While AvailableProgramOutput(winscp_handle)
    PrintN(ReadProgramString(winscp_handle,#PB_Ascii))
  Wend
  ClearStructure(*Parameters, Command)
  FreeMemory(*Parameters)
  Debug "Command line completed"
  PrintN("Command line completed ("+string$+") - Enter exit to quit.")
EndProcedure

If console And winscp_handle
  ;;;EnableGraphicalConsole(#True)
  ConsoleCursor(1)
  ConsoleColor(15,0)
  If OpenWindow(0,0,0,100,100,#Empty$,#PB_Window_Invisible)
    PrintN(#Empty$)
    PrintN("Welcome to WinScp console.")
    PrintN(#Empty$)
    PrintN("winscp> Enter ? to displays help or exit to quit.")
    PrintN(#Empty$)
    WriteProgramStringN(winscp_handle,#Empty$)
    Print(ReadProgramString(winscp_handle,#PB_Ascii))
    Repeat
      Event=WaitWindowEvent(10)
      string$=Input()
      If string$<>#Empty$
        Select LCase(string$)
          Case "exit"
            Break
          Case "cls"
            EnableGraphicalConsole(#True)
            ClearConsole()
            EnableGraphicalConsole(#False)
            WriteProgramStringN(winscp_handle,#Empty$)
            Print(ReadProgramString(winscp_handle,#PB_Ascii))
            string$=#Empty$
          Case "?"
            string$="help"
          Default
            ;;;string$=#Empty$
        EndSelect
        If string$<>#Empty$
          *Parameters.Command = AllocateMemory(SizeOf(Command))
          *Parameters\Command$ = string$
          Thread = CreateThread(@sendCommand(), *Parameters)
          WaitThread(Thread, 1500)
          WriteProgramStringN(winscp_handle,#CRLF$+#CRLF$)
          Delay(50)
          Print(ReadProgramString(winscp_handle,#PB_Ascii))
        EndIf
      Else
        ;;;Delay(100)
        WriteProgramStringN(winscp_handle,#CRLF$)
        Print(ReadProgramString(winscp_handle,#PB_Ascii))
      EndIf
      string$=#Empty$
    ForEver
  EndIf
  CloseConsole()
EndIf
Gérard

Re: RunProgram with #PB_Program_Hide not working as expected

Posted: Thu Aug 17, 2023 4:54 pm
by dave@idea-tech.com
Thank You very much Gerard!

I will study this carefully.