Read console output

Just starting out? Need help? Post your questions and find answers here.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Read console output

Post by ricardo »

See this example:

Code: Select all

If OpenConsole()
  ConsoleTitle("Lets see")
  ApplicationName.s = "cmd.exe /c ping google.com"
  If WinExec_(@ApplicationName.s, #SW_SHOWNORMAL) = 0  
    MessageRequester("ERROR", Str(GetLastError_()) , #PB_MessageRequester_Ok)
  EndIf
  Repeat  
    Delay(20)
  Until GetAsyncKeyState_(#VK_ESCAPE)
EndIf
End
Im trying to read the output but cant.

Yes, i know i can run it from runprogram. But the problem is that i found some commandline that dont let me catch the output by that way and im trying to see if i can do it by this way.

Thanks in advance for any help
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Code: Select all

EnableExplicit

Define ping.l = RunProgram("ping", "google.com", "", #PB_Program_Open|#PB_Program_Read)
Define Output.s = ""
If ping
  While ProgramRunning(ping)
    Output + ReadProgramString(ping) + Chr(13)
  Wend
  CloseProgram(ping)
  Output + Chr(13) + Chr(13)
EndIf
MessageRequester("ping", Output)
greetings
Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

ts-soft wrote:

Code: Select all

EnableExplicit

Define ping.l = RunProgram("ping", "google.com", "", #PB_Program_Open|#PB_Program_Read)
Define Output.s = ""
If ping
  While ProgramRunning(ping)
    Output + ReadProgramString(ping) + Chr(13)
  Wend
  CloseProgram(ping)
  Output + Chr(13) + Chr(13)
EndIf
MessageRequester("ping", Output)
greetings
Thomas

Thanks, but...
From the original post:
Yes, i know i can run it from runprogram. But the problem is that i found some commandline that dont let me catch the output by that way and im trying to see if i can do it by this way.
:twisted:

*However, even for curiosity, how can i read it from my own console like the example i show?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Okay, I overlooked or my English is not as good :oops:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Im trying to read the output
No, you're not. None of your code has anything to do with reading the output.

We can make a solution that works for the other program unless we know how it behaves. Ping works ok with ts-soft's code.
andyryu
New User
New User
Posts: 7
Joined: Mon Jul 30, 2007 7:58 am
Location: Shanghai,China

Post by andyryu »

you can use write and read pipe with winapi,then readfile from pipe
but i think purebasic do it same way
some exe redirect output ,you can catch it
Post Reply