-
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.
-
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"
-
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.
-
Re: A program that launches console applications and captures their output.
I am using Windows2000 server Edition :)
-
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.
-
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?
-
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?
-
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...
-
Re: A program that launches console applications and captures their output.
EDIT: See my latest post for the attachment.
-
Re: A program that launches console applications and captures their output.
Thanks, i'll try it later
-
Re: A program that launches console applications and captures their output.
Uploaded a major update. Attachment: Console Shell.zip.
-
Re: A program that launches console applications and captures their output.
Quote:
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
-
Re: A program that launches console applications and captures their output.
-
Re: A program that launches console applications and captures their output.
Quote:
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
-
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?
-
Re: A program that launches console applications and captures their output.
Quote:
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
-
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.
-
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.
-
Re: A program that launches console applications and captures their output.
Quote:
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>
-
Re: A program that launches console applications and captures their output.
Quote:
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 :(
Quote:
Originally Posted by
Peter Swinkels
I'll check it, thanks
Quote:
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!
-
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.
-
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!!