-
Dec 30th, 2006, 03:35 PM
#1
Thread Starter
Frenzied Member
A program that launches console applications and captures their output.
EDIT: See my latest post for the most recent version of the code.
Console Shell is a program that demonstrates how to capture the output of a console application. It uses the CreateProcess API function to launch a console application. The input/output is redirected by specifying handles to an output file and a console created by my program in the STARTUPINFO structure.
Last edited by Peter Swinkels; Nov 14th, 2012 at 09:57 AM.
-
Jan 3rd, 2007, 12:36 AM
#2
Re: A program that launches console applications and captures their output.
Dear Peter Swinkels
Thanks for the post.But I am getting this error.
"Can't Find DLL Entry Point GetConsoleprocessList in Kernel32.dll"
Please mark you thread resolved using the Thread Tools as shown
-
Jan 4th, 2007, 08:07 AM
#3
Thread Starter
Frenzied Member
Re: A program that launches console applications and captures their output.
What version of Windows are you using? Yours might not support that API... Windows Vista and XP support it.
Or, are you trying to write your own program using a declaration for GetConsoleprocessList ? If so, watch the capitalisation, because it should be GetConsoleProcessList. API function names are case sensitive.
-
Jan 4th, 2007, 08:25 AM
#4
Re: A program that launches console applications and captures their output.
I am using Windows2000 server Edition
Please mark you thread resolved using the Thread Tools as shown
-
Jan 4th, 2007, 08:27 AM
#5
Thread Starter
Frenzied Member
Re: A program that launches console applications and captures their output.
That probably is the problem. Perhaps you should check the MSDN, just to be sure.
-
Jan 25th, 2008, 07:44 PM
#6
Junior Member
Re: A program that launches console applications and captures their output.
can this code refresh each second to reflect changes to the captured console? and only capture lines (not the whole console content) and lastly, we can cancel (or shutdown) the captured console app?
-
Jan 26th, 2008, 07:19 AM
#7
Thread Starter
Frenzied Member
Re: A program that launches console applications and captures their output.
I'm not sure whether the code I attached to this thread is suitable for your purposes. Could you explain what kind of an application you are trying to make and what exactly it needs to do with the captured console output?
-
Jan 26th, 2008, 09:40 PM
#8
Junior Member
Re: A program that launches console applications and captures their output.
ok, I have quite number of console apps that launches after each another... I can just run them, but it doesn't seems nice if it shows on the screen, so I figure it would be better to show the output on a textbox object.
I just wish that the captured console can be terminated because some console apps that I have run for a long period of time... and it can refresh the console output each second without locking the app. I found out when a console app output is captured, it locked the calling app until the console is done with what it was doing...
-
Jan 27th, 2008, 05:11 AM
#9
Thread Starter
Frenzied Member
Re: A program that launches console applications and captures their output.
EDIT: See my latest post for the attachment.
Last edited by Peter Swinkels; Nov 14th, 2012 at 09:47 AM.
-
Jan 27th, 2008, 05:28 AM
#10
Junior Member
Re: A program that launches console applications and captures their output.
Thanks, i'll try it later
-
Nov 14th, 2012, 09:44 AM
#11
Thread Starter
Frenzied Member
Re: A program that launches console applications and captures their output.
Uploaded a major update. Attachment: Console Shell.zip.
Last edited by Peter Swinkels; Nov 15th, 2015 at 01:58 PM.
Reason: Improved error handling and cleaner code.
-
Dec 29th, 2021, 01:21 PM
#12
New Member
Re: A program that launches console applications and captures their output.
Originally Posted by Peter Swinkels
Uploaded a major update. Attachment: Console Shell.zip.
Hi, sorry for bringing up an old post...
I cant find the attach :/
thanks
-
Dec 29th, 2021, 01:23 PM
#13
Thread Starter
Frenzied Member
Re: A program that launches console applications and captures their output.
-
Dec 29th, 2021, 02:16 PM
#14
New Member
Re: A program that launches console applications and captures their output.
Originally Posted by Peter Swinkels
thanks!!
I tried it, I have the same problem, like my code...
I must wait for the end of executed software, to get the output...
can it be readed in real time?
thanks again
-
Dec 29th, 2021, 04:38 PM
#15
Thread Starter
Frenzied Member
Re: A program that launches console applications and captures their output.
@nicogalan:
You already have your own code? Could you post it so I can look at it?
-
Dec 29th, 2021, 08:07 PM
#16
New Member
Re: A program that launches console applications and captures their output.
Originally Posted by Peter Swinkels
@nicogalan:
You already have your own code? Could you post it so I can look at it?
Hi, yes, I have this:
Code:
Function ejecutar_Dos(Comando As String) As String
Dim oShell As WshShell
Dim oExec As WshExec
Dim Ret As String
Set oShell = New WshShell
DoEvents
' ejecutar el comando
Set oExec = oShell.Exec("%comspec% /c " & Comando)
Ret = oExec.StdErr.ReadAll()
' retornar la salida y devolverla a la función
ejecutar_Dos = Ret ' Replace(ret, Chr(10), vbNewLine)
DoEvents
Me.SetFocus
End Function
Function ejecutar_Dos2(Comando As String) As String
Dim oShell As WshShell
Dim oExec As WshExec
Dim Ret As String
Set oShell = New WshShell
DoEvents
' ejecutar el comando
Set oExec = oShell.Exec("%comspec% /c " & Comando)
Ret = oExec.StdOut.ReadAll()
' retornar la salida y devolverla a la función
ejecutar_Dos2 = Ret ' Replace(ret, Chr(10), vbNewLine)
DoEvents
Me.SetFocus
End Function
Private Sub Command1_Click()
Dim Exec As String
Exec = ("myexecutedsoft.exe")
'txt_resultado.Text = ejecutar_Dos(Trim(Exec))
txt_resultado.Text = ejecutar_Dos2(Trim(Exec))
End Sub
this code, executes the exe I need, and returns the result on my txt, but, I must wait for the whole process to be finished...
I need to see it in real time, or, be able to see whats happening on the executed exe...
this way, myexecutedsoft.exe seems not responding, only cmd screen, but I cant see whats happening, until it ends...
thanks
ps: I also tried with "shell" but I cant log to textbox what myexecutedsoft.exe returns
Last edited by nicogalan; Dec 29th, 2021 at 08:12 PM.
-
Dec 29th, 2021, 08:25 PM
#17
Re: A program that launches console applications and captures their output.
I think that is a DOS/cmd.exe restriction.
Linux and Unix use pipes that create a stream in memory and feeds the output of one program to the other directly.
DOS and the cmd.exe version implement pipes using temporary files. The output of one executable is redirected to a file. When that program ends then that file is fed into the piped executable. I assume this was because DOS and cmd.exe were not designed to be multi-threaded so didn't have an easy way to implement parallel running executables to allow "true" piping between simultaneous running programs.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Dec 30th, 2021, 05:09 AM
#18
Thread Starter
Frenzied Member
Re: A program that launches console applications and captures their output.
This is for C# but may point you in the right direction:
https://stackoverflow.com/questions/...-application-c
Otherwise I have no idea.
-
Dec 30th, 2021, 05:46 AM
#19
Re: A program that launches console applications and captures their output.
Originally Posted by nicogalan
this code, executes the exe I need, and returns the result on my txt, but, I must wait for the whole process to be finished...
This is because you use Ret = oExec.StdOut.ReadAll() to read *all* the output. The ReadAll method will wait for the program to finish so that it's sure *all* the output is captured.
Just use ReadLine or Read methods on the StdOut to read lines/chunks of output *while* the program is running.
StdOut is a TextStream object so you have a lot of other useful methods/properties there like SkipLine or AtEndOfStream -- check out the link for more.
cheers,
</wqw>
-
Dec 30th, 2021, 09:14 AM
#20
New Member
Re: A program that launches console applications and captures their output.
Originally Posted by passel
I think that is a DOS/cmd.exe restriction.
Linux and Unix use pipes that create a stream in memory and feeds the output of one program to the other directly.
DOS and the cmd.exe version implement pipes using temporary files. The output of one executable is redirected to a file. When that program ends then that file is fed into the piped executable. I assume this was because DOS and cmd.exe were not designed to be multi-threaded so didn't have an easy way to implement parallel running executables to allow "true" piping between simultaneous running programs.
wow, this is a a pain....
sadly, the app wont run on linux
Originally Posted by Peter Swinkels
I'll check it, thanks
Originally Posted by wqweto
This is because you use Ret = oExec.StdOut.ReadAll() to read *all* the output. The ReadAll method will wait for the program to finish so that it's sure *all* the output is captured.
Just use ReadLine or Read methods on the StdOut to read lines/chunks of output *while* the program is running.
StdOut is a TextStream object so you have a lot of other useful methods/properties there like SkipLine or AtEndOfStream -- check out the link for more.
cheers,
</wqw>
I tried readline, and same situation...
it waits for the programm to finish, to receive just 1 line of output.
will check the info you sent me too
thanks!
-
Dec 30th, 2021, 10:23 AM
#21
Re: A program that launches console applications and captures their output.
Look this https://www.vbforums.com/showthread....=1#post5494666
I got the ShellPipe control from dilettante and change it to a class with events, to use it for late binding in M2000 Interpreter.
I use this, without events, in a chess program, to send a FEN string (the current position in chess board), and to receive an answer. I use a loop every 50 ms, to call the ProcessLoop method, and to check if it is Active and if it has Line.
So this function GetMoves() return an array (tuple) when engine send a string which have bestmove. Because for my chess program there is no other job to do, this converted to a sync task. In this loop also I check if the exe is running. So after the function return, the exe waiting and not exit. When the Engine object (a ShellPipe) get out of scope then terminate the exe.
PHP Code:
Function GetMove$(Fen$) local aLine$ method Engine, "SendLine" , "position fen "+Fen$ method Engine, "SendLine" ,"go movetime"+str$(random(50, 200)) every 50 { Method Engine, "ProcessLoop" if not Engine.Active then exit while Engine.HasLine method Engine, "GetLine" as aLine$ if left$(aLine$,8)="bestmove" then exit End While if left$(aLine$,8)="bestmove" then =piece$(aLine$," ", 2) : exit } End Function
The implementation of Every statement is here:
https://raw.githubusercontent.com/M2...n/Mod_Text.bas
Open as raw and search "EVERY" (there is some characters not show correctly, because is the greek identifier for this statement). To compile the M2000.dll you have to set font to Courier New Greek, and the open the project.
Last edited by georgekar; Dec 30th, 2021 at 10:30 AM.
-
Dec 30th, 2021, 11:56 PM
#22
New Member
Re: A program that launches console applications and captures their output.
Thanks everybody!
sadly, read the output lines, was impossible..... :/
I also tried C# code, the same result... always shelled app needed to finish to receive the output...
what was good to me was simply shell (cmd /k and my app to shell and arguments)
this way, I can read the output in real time, and app wont close after finish
thanks again!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|