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

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

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

Post 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
Last edited by Kwai chang caine on Fri Aug 03, 2012 4:12 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
peterb
User
User
Posts: 60
Joined: Sun Oct 02, 2005 8:55 am
Location: Czech Republic
Contact:

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

Post by peterb »

You can try this

Code: Select all

One = RunProgram("cmd")
Delay(1000)
Two = RunProgram("cmd.exe", "/c telnet", "" )
peterb
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

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

Post 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)
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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.
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

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

Post 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
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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 ???
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

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

Post 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)
ImageThe happiness is a road...
Not a destination
Post Reply