How to use #PB_Program_Connect

Just starting out? Need help? Post your questions and find answers here.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

How to use #PB_Program_Connect

Post by kenmo »

The Help for RunProgram() says:
#PB_Program_Connect: Connect another programs output to this programs input.

When using the #PB_Program_Connect flag, another program must have been started before with #PB_Program_Open and #PB_Program_Read. The number returned when running this program must be passed as the 'SenderProgram' parameter to RunProgram(). The output of the sender program will be sent directly to the input of the now executed program. Several programs may be connected in this way, to 'pipe' data through that group of connected programs.
Does anyone have an example of using this? I tried to pipe ipconfig's output to findstr (on Windows) based on this thread:
viewtopic.php?f=13&t=73438

Here is my test code, can we get #PB_Program_Connect working, or is this a Bug Report?

Code: Select all

CompilerIf (#PB_Compiler_ExecutableFormat <> #PB_Compiler_Console)
  ;CompilerError "Please compile in Console mode" ; Maybe Console mode is needed?
CompilerEndIf
CompilerIf (#PB_Compiler_OS <> #PB_OS_Windows)
  CompilerError "This example is designed for Windows"
CompilerEndIf

If OpenConsole() ; Is OpenConsole() needed?
 
  Flags = #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide
  *ipconfig = RunProgram("ipconfig", "/all", "", Flags)
  ;Debug *ipconfig
 
  Flags = #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide | #PB_Program_Connect ; What flags are needed with Connect?
  *findstr = RunProgram("findstr", "/I ipv4", "", Flags, *ipconfig)
  ;Debug *findstr
 
  While (ProgramRunning(*ipconfig) Or ProgramRunning(*findstr))
    If AvailableProgramOutput(*ipconfig) ; Shouldn't the ipconfig output automatically go to findstr?
      Output.s + ReadProgramString(*ipconfig) + #CRLF$
    EndIf
    If AvailableProgramOutput(*findstr)
      Output2.s + ReadProgramString(*findstr) + #CRLF$
    EndIf
    Delay(0)
  Wend
 
  Debug "-------------- ipconfig output:"
  Debug Output
  Debug "-------------- findstr output:"
  Debug Output2
 
  CloseProgram(*findstr)
  CloseProgram(*ipconfig)
 
  CloseConsole()
EndIf
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How to use #PB_Program_Connect

Post by infratec »

Hi,

I can show you an example, but ...

this will not help you, since ipconfig is not 'build' to use it this way.

If you use RunProgram() to start it, it runs and finish.
It does not send something continiously.
So before you start findtsr the output is gone.

The other way round: findtstr does not wait for an input.

So the only way is to use one 'command' combined with cmd as shell to achive what you want.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: How to use #PB_Program_Connect

Post by #NULL »

I'm trying it on linux (with ifconfig and grep) and I can't get any output from grep either.

<edit>
but grep has an ProgramExitCode() of 2.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: How to use #PB_Program_Connect

Post by kenmo »

So maybe I'm not testing it with programs that work with #PB_Program_Connect? I only used ipconfig and findstr because that's what was requested in the thread I linked.
this will not help you, since ipconfig is not 'build' to use it this way.

If you use RunProgram() to start it, it runs and finish.
It does not send something continiously.
So before you start findtsr the output is gone.
Shouldn't the output be buffered somewhere... OS or PB level... until it's read out by ReadProgramString() or similar?
My test code gets all the output, it just doesn't route through the findstr process.
The other way round: findtstr does not wait for an input.
I think it does. Open a cmd prompt and run "findstr /I ipv4", then you can type lines of text to it and it will echo back lines with "ipv4" in sort-of-realtime, until you quit with a Ctrl+Z terminator.


Anyway, what is cmd doing internally? It must be something similar, starting ipconfig and findstr processes and linking them together somehow. I'm not a console/pipe expert, so I'm just trying to understand.
BarryG
Addict
Addict
Posts: 3293
Joined: Thu Apr 18, 2019 8:17 am

Re: How to use #PB_Program_Connect

Post by BarryG »

kenmo wrote:I only used ipconfig and findstr because that's what was requested in the thread I linked.
I posted a reply in that thread, that might work: viewtopic.php?p=540813#p540813
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: How to use #PB_Program_Connect

Post by kenmo »

I posted a reply in that thread, that might work: viewtopic.php?p=540813#p540813
I saw that, clever solution, works perfectly for his question :D


I would still like to know if #PB_Program_Connect is broken, or if I'm using it wrong. Here's a PB-only test, it does the same thing as ipconfig/findstr (the output of the "producer" program doesn't seem to connect to the "consumer" program)

Code: Select all

CompilerIf (#PB_Compiler_ExecutableFormat <> #PB_Compiler_Console)
  CompilerError "Run in Console mode"
CompilerEndIf

#N = 5

If (OpenConsole())
  
  If (ProgramParameter(0) = "") ; Main Program
    Flags = #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide
    *Producer = RunProgram(ProgramFilename(), "-producer", "", Flags)
    Flags = #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide | #PB_Program_Connect
    *Consumer = RunProgram(ProgramFilename(), "-consumer", "", Flags, *Producer)
    
    PrintN("*Producer: " + Hex(*Producer))
    PrintN("*Consumer: " + Hex(*Consumer))
    PrintN("*Producer is sending " + Str(#N) + " lines to *Consumer...")
    PrintN("Please wait...")
    While (ProgramRunning(*Producer) Or ProgramRunning(*Consumer))
      If (AvailableProgramOutput(*Producer))
        Output.s + ReadProgramString(*Producer) + #CRLF$
      EndIf
      If (AvailableProgramOutput(*Consumer))
        Output2.s + ReadProgramString(*Consumer) + #CRLF$
      EndIf
      Delay(1)
    Wend
    PrintN("")
    PrintN("Producer output:")
    PrintN(Output)
    PrintN("Consumer output:")
    PrintN(Output2)
    CloseProgram(*Consumer)
    CloseProgram(*Producer)
    PrintN("Press Enter to exit")
    Input()
    
  ElseIf (ProgramParameter(0) = "-producer") ; Producer Program
    For i = 1 To #N
      Delay(1000)
      PrintN("Hello " + Str(i))
    Next i
    
  ElseIf (ProgramParameter(0) = "-consumer") ; Consumer Program
    *Buffer = AllocateMemory(1000)
    For i = 1 To #N
      Delay(1000)
      If ReadConsoleData(*Buffer, 1000)
        PrintN(PeekS(*Buffer, -1, #PB_UTF8))
        FillMemory(*Buffer, 1000, #NUL)
      EndIf
    Next i
    FreeMemory(*Buffer)
  EndIf
  
  CloseConsole()
EndIf
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: How to use #PB_Program_Connect

Post by applePi »

#PB_Program_Connect ? : it is probably a forgotten experiment from betas, and does not have any purpose
look with #PB_Program_Connect or without we will get the same result
Volume in drive C is part2 Volume Serial Number is 82A1-07CF Directory of C:\Documents and Settings\.... 02:30 PM 1,147,319 20 File(s) 14,397,429 bytes 4 Dir(s) 26,677,690,368 bytes free

Code: Select all

console.l = RunProgram("Dir.exe"," -s *.*","",#PB_Program_Connect|#PB_Program_Open|#PB_Program_Read|#PB_Program_Write) 

While ProgramRunning(console)
 s.s = s.s + "    " + ReadProgramString(console) 
;Delay(10) 
Wend
Debug s
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: How to use #PB_Program_Connect

Post by kenmo »

It has been in the help file for 13 years (v4.0), so I hope it's not a beta feature!
http://www.purearea.net/pb/english/hist ... hanges.htm

It's possible that it broke in some version, but it's uncommon so nobody realized.
Also possible, it works, and I just don't know how to use it :)
applePi wrote:look with #PB_Program_Connect or without we will get the same result
You're not giving it a *SenderProgram which it says is required, so I don't expect that test to work.
Post Reply