Page 1 of 1

Have two windows of Cmd.exe for two call [Resolved]

Posted: Fri Aug 03, 2012 10:48 am
by Kwai chang caine
Hello

I see today a strange behaviour :shock:

If i run this code, all works

Code: Select all

One = RunProgram("cmd")
Delay(1000)
Two = RunProgram("Telnet")
I have two windows, one of Cmd.exe, one of Telnet 8)

But if i want have the StdIn/Out

Code: Select all

One = RunProgram("cmd")
Delay(1000)
Two = RunProgram("Telnet", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write)
I have just the first windows...no Telnet :(

How can i do for have two windows ??

Good day

Re: Have two windows of Cmd.exe for two call

Posted: Fri Aug 03, 2012 11:18 am
by peterb
You can try this

Code: Select all

One = RunProgram("cmd")
Delay(1000)
Two = RunProgram("cmd.exe", "/c telnet", "" )
peterb

Re: Have two windows of Cmd.exe for two call

Posted: Fri Aug 03, 2012 11:44 am
by Kwai chang caine
Thanks PeterB
I have try your tips, but like in my example, if i add the ReadProgram..that's not works, i have always the first window :(

Code: Select all

One = RunProgram("cmd")
Delay(1000)
Two = RunProgram("cmd.exe", "/c telnet", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write)

Re: Have two windows of Cmd.exe for two call

Posted: Fri Aug 03, 2012 1:12 pm
by infratec
Hi KCC,

Code: Select all

#ProgramRunning = 0

One = RunProgram("cmd")
Two = RunProgram("cmd", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write)

Counter = 0
CompilerIf #ProgramRunning = 1
  While ProgramRunning(Two)
CompilerElse
  While IsProgram(Two)
CompilerEndIf
  Delay(1)
  Counter + 1
  Debug Counter
  If Counter = 3000
    WriteProgramStringN(Two, "exit")
    Debug "Exit"
CompilerIf #ProgramRunning = 0    
    Delay(1000)
    Debug "Close"
    CloseProgram(Two)
CompilerEndIf
  EndIf
Wend
CompilerIf #ProgramRunning = 1
  CloseProgram(Two)
CompilerEndIf
I found a strange behaviour of ProgramRunning():
It did not return 0, in this case, if the program is finished.

Bernd

Re: Have two windows of Cmd.exe for two call

Posted: Fri Aug 03, 2012 1:40 pm
by infratec
Hi,

I found the trouble maker:

Code: Select all

Prog = RunProgram("cmd", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write)

If IsProgram(Prog)
  Counter = 0
  While ProgramRunning(Prog)
    Delay(1)
    Counter + 1
    Debug Counter
    If Counter = 3000
      WriteProgramStringN(Prog, "exit")
      Debug "Exit"
     Stdout.a = 0
     Repeat
       Bytes = AvailableProgramOutput(Prog)
       If Bytes
         ReadProgramData(Prog, @Stdout, 1)
         Debug Chr(Stdout)
       EndIf
     Until Bytes = 0        
    EndIf
  Wend
  CloseProgram(Prog)
EndIf
The program is somehow still active for PB if there is data to read.
If the data buffer is read out, ProgrammRunning() terminates as expected.

Bernd

P.S.: I'm not sure if this is a bug or to less documentation in the help.
But even SysInternals ProcessExplorer shows that cmd is finished.
And still it is running for PB.

Re: Have two windows of Cmd.exe for two call

Posted: Fri Aug 03, 2012 1:51 pm
by infratec
Or you simply read always the output

Code: Select all

Prog = RunProgram("cmd", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write)

If IsProgram(Prog)
  Counter = 0
  Output$ = ""
  Stdout.a = 0
  While ProgramRunning(Prog)
    
    If AvailableProgramOutput(Prog)
      ReadProgramData(Prog, @Stdout, 1)
      Output$ + Chr(Stdout)
      If Stdout = #LF
        Debug Output$
        Output$ = ""
      EndIf
    EndIf
    
    Delay(1)
    Counter + 1
    If Counter = 3000
      WriteProgramStringN(Prog, "exit")
      Debug "Exit"
    EndIf
  Wend
  CloseProgram(Prog)
EndIf
Bernd

Re: Have two windows of Cmd.exe for two call

Posted: Fri Aug 03, 2012 1:53 pm
by Kwai chang caine
Thanks Infratec 8)

But how can i have two windows with the prompt ??
One with no read Output, and one like yours

Code: Select all

Prog2 = RunProgram("cmd")
Prog = RunProgram("cmd", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write)

If IsProgram(Prog)
  Counter = 0
  While ProgramRunning(Prog)
    Delay(1)
    Counter + 1
    Debug Counter
     
    If Counter = 3000
      WriteProgramStringN(Prog, "exit")
      Debug "Exit"
     Stdout.a = 0
     Repeat
       Bytes = AvailableProgramOutput(Prog)
       If Bytes
         ReadProgramData(Prog, @Stdout, 1)
         Debug Chr(Stdout)
       EndIf
     Until Bytes = 0        
    EndIf
  Wend
  CloseProgram(Prog)
EndIf

Re: Have two windows of Cmd.exe for two call

Posted: Fri Aug 03, 2012 2:08 pm
by infratec
Hi KCC,

With your code, I have 2 cmd windows.
But over one is no control.

The problems start if you want to run 2 cmd with read write possibilities.

Than PB closes one immediately.


Bug ???

Re: Have two windows of Cmd.exe for two call

Posted: Fri Aug 03, 2012 2:15 pm
by infratec
Maybe soemthing like this:

Code: Select all

KCC = RunProgram("cmd", "", "", #PB_Program_Open)

Prog = RunProgram("cmd", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write)

If IsProgram(Prog)
  Counter = 0
  Output$ = ""
  Stdout.a = 0
  While ProgramRunning(Prog)
    
    If AvailableProgramOutput(Prog)
      ReadProgramData(Prog, @Stdout, 1)
      Output$ + Chr(Stdout)
      If Stdout = #LF
        Debug Output$
        Output$ = ""
      EndIf
    EndIf
    
    Delay(1)
    Counter + 1
    If Counter = 3000
      WriteProgramStringN(Prog, "exit")
      Debug "Exit"
    EndIf
  Wend
  CloseProgram(Prog)
EndIf

If IsProgram(KCC)
  KillProgram(KCC)
  Debug "KCC is killed now ! :)"
  CloseProgram(KCC)
EndIf
Bernd

Re: Have two windows of Cmd.exe for two call

Posted: Fri Aug 03, 2012 2:38 pm
by Kwai chang caine
The problems start if you want to run 2 cmd with read write possibilities.
Yes it's that the problem...
It's strange ...
Perhaps it's impossible to have two CMD in the same time, with one with Read/Write :(

So thanks for your help
I wish you a good day 8)