Results 1 to 22 of 22

Thread: A program that launches console applications and captures their output.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.

  6. #6
    Junior Member
    Join Date
    Jan 2008
    Posts
    16

    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?

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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?

  8. #8
    Junior Member
    Join Date
    Jan 2008
    Posts
    16

    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...

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.

  10. #10
    Junior Member
    Join Date
    Jan 2008
    Posts
    16

    Re: A program that launches console applications and captures their output.

    Thanks, i'll try it later

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.

  12. #12
    New Member
    Join Date
    Dec 2021
    Posts
    5

    Re: A program that launches console applications and captures their output.

    Quote Originally Posted by Peter Swinkels View Post
    Uploaded a major update. Attachment: Console Shell.zip.
    Hi, sorry for bringing up an old post...
    I cant find the attach :/

    thanks

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: A program that launches console applications and captures their output.


  14. #14
    New Member
    Join Date
    Dec 2021
    Posts
    5

    Re: A program that launches console applications and captures their output.

    Quote Originally Posted by Peter Swinkels View Post
    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

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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?

  16. #16
    New Member
    Join Date
    Dec 2021
    Posts
    5

    Re: A program that launches console applications and captures their output.

    Quote Originally Posted by Peter Swinkels View Post
    @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.

  17. #17
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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

  18. #18

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.

  19. #19
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: A program that launches console applications and captures their output.

    Quote Originally Posted by nicogalan View Post
    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>

  20. #20
    New Member
    Join Date
    Dec 2021
    Posts
    5

    Re: A program that launches console applications and captures their output.

    Quote Originally Posted by passel View Post
    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 View Post
    This is for C# but may point you in the right direction:
    https://stackoverflow.com/questions/...-application-c

    Otherwise I have no idea.
    I'll check it, thanks

    Quote Originally Posted by wqweto View Post
    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!

  21. #21
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    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(50200))
        
    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.

  22. #22
    New Member
    Join Date
    Dec 2021
    Posts
    5

    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
  •  



Click Here to Expand Forum to Full Width