[Implemented] Run DOS and get output
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
[Implemented] Run DOS and get output
Restored from previous forum. Originally posted by theogott.
The chnages to PB 2.70 with the possibility to start a program and wait for it to end are a great advantage to that what other languages offer.
I am just mising one thing. How can I start a program (A DOS Program), and get
the programms output ?
(I dont want to use the ">" output redirection).
What I'd like to have is a
A$=runDOSprogram("DIR C:")
and A$ should contain the output from the command.
If someone has any ideas about that, please mail me: [url]mailto:atp@c10.de[/url]
(Implemented with 'ReadProgramString()')
*************************
The best time to do things is now !
The chnages to PB 2.70 with the possibility to start a program and wait for it to end are a great advantage to that what other languages offer.
I am just mising one thing. How can I start a program (A DOS Program), and get
the programms output ?
(I dont want to use the ">" output redirection).
What I'd like to have is a
A$=runDOSprogram("DIR C:")
and A$ should contain the output from the command.
If someone has any ideas about that, please mail me: [url]mailto:atp@c10.de[/url]
(Implemented with 'ReadProgramString()')
*************************
The best time to do things is now !
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by ricardo.
There MANY reasons to do it, for stability, for speed, etc.
If your DOS program gives a lot of output, an out file will be hughe and you will need to read only the last lines, etc.
By reading directly from the 'DOS screen' will be more secure and speed.
I search exactly THIS answer so many time and i has found the answer,
its so usefull...
I found this answer for PowerBasic, and never tested in PureBasic, but since its only API, im sure its easier to run it on PureBasic than in other compilers, because PureBasic don't give problems with the type of parameters : )
Here is the POWERBASIC code, i want to translate it to PureBasic but don't know how to manage CONSOLE_SCREEN_BUFFER_INFO, maybe some one could tell us how to include the structure or if it is on any win32API.inc or something
'********************************************************** CONSOLE TO BUFFER
'// Retrieves all textlines from the current console window.
Function ConsoleToBuffer() As String
Dim a As Long
Dim hCons As Long
Dim Result As Long
Dim nBytes As Long
Dim sScreen As String
Dim sBuffer As String
Dim SBI As CONSOLE_SCREEN_BUFFER_INFO
'// Get handle to console.
hCons = GetStdHandle( %STD_OUTPUT_HANDLE )
If hCons = 0 Then Exit Function
'// Get console size
Call GetConsoleScreenBufferInfo( hCons, SBI )
'// Loop through all lines.
For a = 0 To SBI.dwSize.Y - 1
'// Prepare buffer for this line.
sBuffer = Space$( SBI.dwSize.X )
'// Read the line.
Result = ReadConsoleOutputCharacter( _
hCons _
, ByVal StrPtr( sBuffer ) _
, Len( sBuffer ) _
, ByVal MakLng( 0, a ) _
, nBytes )
If Result Then sScreen = sScreen & Rtrim$( sBuffer ) & $CRLF
Next a
'// Return lines.
Function = sScreen
End Function
It sure function, i used so many times
Regards
There MANY reasons to do it, for stability, for speed, etc.
If your DOS program gives a lot of output, an out file will be hughe and you will need to read only the last lines, etc.
By reading directly from the 'DOS screen' will be more secure and speed.
I search exactly THIS answer so many time and i has found the answer,
its so usefull...
I found this answer for PowerBasic, and never tested in PureBasic, but since its only API, im sure its easier to run it on PureBasic than in other compilers, because PureBasic don't give problems with the type of parameters : )
Here is the POWERBASIC code, i want to translate it to PureBasic but don't know how to manage CONSOLE_SCREEN_BUFFER_INFO, maybe some one could tell us how to include the structure or if it is on any win32API.inc or something
'********************************************************** CONSOLE TO BUFFER
'// Retrieves all textlines from the current console window.
Function ConsoleToBuffer() As String
Dim a As Long
Dim hCons As Long
Dim Result As Long
Dim nBytes As Long
Dim sScreen As String
Dim sBuffer As String
Dim SBI As CONSOLE_SCREEN_BUFFER_INFO
'// Get handle to console.
hCons = GetStdHandle( %STD_OUTPUT_HANDLE )
If hCons = 0 Then Exit Function
'// Get console size
Call GetConsoleScreenBufferInfo( hCons, SBI )
'// Loop through all lines.
For a = 0 To SBI.dwSize.Y - 1
'// Prepare buffer for this line.
sBuffer = Space$( SBI.dwSize.X )
'// Read the line.
Result = ReadConsoleOutputCharacter( _
hCons _
, ByVal StrPtr( sBuffer ) _
, Len( sBuffer ) _
, ByVal MakLng( 0, a ) _
, nBytes )
If Result Then sScreen = sScreen & Rtrim$( sBuffer ) & $CRLF
Next a
'// Return lines.
Function = sScreen
End Function
It sure function, i used so many times
Regards
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by wavemaker.
Hola, Ricardo. It seems to work:
Juan Calderón Alonso
Registered user
Hola, Ricardo. It seems to work:
Code: Select all
OpenDefaultConsole()
PrintN("Hello, world!")
PrintN("This is only a test,")
PrintN("we're trying to read")
PrintN("from the console output.")
PrintN("It works!")
;'********************************************************** CONSOLE To BUFFER
;'// Retrieves all textlines from the current console window.
; Note: spaces are also included
nBytes.l
sScreen.s
sBuffer.s
SBI.CONSOLE_SCREEN_BUFFER_INFO
;'// Get handle To console.
hCons = GetStdHandle_(#STD_OUTPUT_HANDLE)
If hCons#INVALID_HANDLE_VALUE
;'// Get console size
GetConsoleScreenBufferInfo_(hCons, @SBI)
;'// Loop through all lines.
dwSizeX = PeekW(@SBI\dwSize)
dwSizeY = PeekW(@SBI\dwSize + 2)
For a = 0 To dwSizeY - 1
;'// Prepare buffer For this line.
sBuffer = Space(dwSizeX)
;'// Read the line.
If ReadConsoleOutputCharacter_(hCons, @sBuffer, Len(sBuffer), a*65536, @nBytes)
sScreen = sScreen + Left(sBuffer, nBytes) + Chr(10) + Chr(13)
EndIf
Next a
;'// Return lines.
MessageRequester("Console window output:", sScreen, 0)
EndIf
CloseConsole()
End
Registered user
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by ricardo.
Hola, Ricardo. It seems to work:
Juan Calderón AlonsoCode: Select all
OpenDefaultConsole() PrintN("Hello, world!") PrintN("This is only a test,") PrintN("we're trying to read") PrintN("from the console output.") PrintN("It works!") ;'********************************************************** CONSOLE To BUFFER ;'// Retrieves all textlines from the current console window. ; Note: spaces are also included nBytes.l sScreen.s sBuffer.s SBI.CONSOLE_SCREEN_BUFFER_INFO ;'// Get handle To console. hCons = GetStdHandle_(#STD_OUTPUT_HANDLE) If hCons#INVALID_HANDLE_VALUE ;'// Get console size GetConsoleScreenBufferInfo_(hCons, @SBI) ;'// Loop through all lines. dwSizeX = PeekW(@SBI\dwSize) dwSizeY = PeekW(@SBI\dwSize + 2) For a = 0 To dwSizeY - 1 ;'// Prepare buffer For this line. sBuffer = Space(dwSizeX) ;'// Read the line. If ReadConsoleOutputCharacter_(hCons, @sBuffer, Len(sBuffer), a*65536, @nBytes) sScreen = sScreen + Left(sBuffer, nBytes) + Chr(10) + Chr(13) EndIf Next a ;'// Return lines. MessageRequester("Console window output:", sScreen, 0) EndIf CloseConsole() End
Registered user
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by ricardo.
muy bien !!!!
very good !!!
But... in XP it dosent runs.
In fact it seems that
OpenDefaultConsole() should be replaced for XP
(logical, XP dosent has DOS)
and
dwSizeX = PeekW(@SBI\dwSize)
dwSizeY = PeekW(@SBI\dwSize + 2)
Crash the application : (
muy bien !!!!
very good !!!
But... in XP it dosent runs.
In fact it seems that
OpenDefaultConsole() should be replaced for XP
(logical, XP dosent has DOS)
and
dwSizeX = PeekW(@SBI\dwSize)
dwSizeY = PeekW(@SBI\dwSize + 2)
Crash the application : (
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by wavemaker.
I don't have Windows XP, so my suggestion would be that you try these:
dwSizeX = (PeekB(@SBI\dwSize + 1)*256) + PeekB(@SBI\dwSize)
dwSizeY = (PeekB(@SBI\dwSize + 3)*256) + PeekB(@SBI\dwSize + 2)
Or:
dwSizeX = (PeekL(@SBI\dwSize)/65536)
dwSizeY = IMod(PeekL(@SBI\dwSize),65536) ; need the MathExtras library for this, sorry
BTW, wishlist: Xor operator, please.
Bye,
Juan Calderón Alonso
Registered user
I don't have Windows XP, so my suggestion would be that you try these:
dwSizeX = (PeekB(@SBI\dwSize + 1)*256) + PeekB(@SBI\dwSize)
dwSizeY = (PeekB(@SBI\dwSize + 3)*256) + PeekB(@SBI\dwSize + 2)
Or:
dwSizeX = (PeekL(@SBI\dwSize)/65536)
dwSizeY = IMod(PeekL(@SBI\dwSize),65536) ; need the MathExtras library for this, sorry
BTW, wishlist: Xor operator, please.
Bye,
Juan Calderón Alonso
Registered user
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by ricardo.
Now im trying to run some external console app from my console app and read
the output... but i dont know how to:
1.- Run the external console app inside my console
2.- Make a timer or something to read a continuosly output from this external console
Help !!!!! Im in a hurry !!!
PS. One more thing... the external console is 16 bit, thats why i cant read it in my PowerBasic code and i think that maybe i can do here.´
One more question... is there any way to open/create/run some external console (hidden) from a DLL and read the continuosly output?
I can give 5 pics of Britney to the guy that helps me
HELPPPPPPPPPPPPPPPPPPPPPP
Now im trying to run some external console app from my console app and read
the output... but i dont know how to:
1.- Run the external console app inside my console
2.- Make a timer or something to read a continuosly output from this external console
Help !!!!! Im in a hurry !!!
PS. One more thing... the external console is 16 bit, thats why i cant read it in my PowerBasic code and i think that maybe i can do here.´
One more question... is there any way to open/create/run some external console (hidden) from a DLL and read the continuosly output?
I can give 5 pics of Britney to the guy that helps me
HELPPPPPPPPPPPPPPPPPPPPPP
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by El_Choni.
Hi, Ricardo,
I don't understand what you mean by "continuously output", but The Execute() function of the CGI library allows redirecting output/input to another program, using the same console (if it is a console app). I don't think that using that from a DLL makes a difference. And I don't know if the 16 bit circumstance changes everything... I just suggest you to try it. If you need some more help, here we are
Bye,
El_Choni
Hi, Ricardo,
I don't understand what you mean by "continuously output", but The Execute() function of the CGI library allows redirecting output/input to another program, using the same console (if it is a console app). I don't think that using that from a DLL makes a difference. And I don't know if the 16 bit circumstance changes everything... I just suggest you to try it. If you need some more help, here we are

Bye,
El_Choni
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by ricardo.
Hi,
If its impossible i will do it from another console...
I appreciate
Ricardo
Hi,
Im trying to get the output from a console that are giving some output during maybe 3 minutes.I don't understand what you mean by "continuously output"
I will try it... where did i can get the CGI lib... Pauls site is off today.The Execute() function of the CGI library allows redirecting output/input to another program, using the same console (if it is a console app).
What i want to do is to call the console from a dll (running the console hidden) and get the output.I don't think that using that from a DLL makes a difference.
If its impossible i will do it from another console...
Really thanks !!!And I don't know if the 16 bit circumstance changes everything... I just suggest you to try it. If you need some more help, here we are![]()
I appreciate
Ricardo
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by El_Choni.
Hi,
The follwing code uses the CGI library. It runs another app in invisible mode and doesn't wait to the termination of the app to return. It also locates the output of the second program at the memory allocation you give it, which you can read at any time.
Hope it helps. Bye,
El_Choni
Hi,
The follwing code uses the CGI library. It runs another app in invisible mode and doesn't wait to the termination of the app to return. It also locates the output of the second program at the memory allocation you give it, which you can read at any time.
Code: Select all
AllocateMemory(0, 4096, 0)
Execute("c:\yourapp.exe", "", "", MemoryID(), 4)
El_Choni