dcsimg
Results 1 to 8 of 8

Thread: Process.StandardOutput not Working with Python Output

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    97

    Process.StandardOutput not Working with Python Output

    Hi, I'm working on a vb.net program that displays the lines from a command prompt. The code is similar to the code below. It works great for all DOS commands that I enter in the command prompt except when I open Python. As you can see in the copy paste that I did below from the command prompt, Python is working fine. However, the program in vb.net doesn't display anything past "c:\python".

    Am I missing something here? Why is the program not displaying the Python output?

    Thank you,

    Robert


    Code:
    Imports System
    Imports System.IO
    Imports System.Diagnostics
    
    Module Module1
        Sub Main()
            Dim process As New Process()
            process.StartInfo.FileName = "cmd.exe"
            process.StartInfo.UseShellExecute = False
            process.StartInfo.RedirectStandardOutput = True
            process.Start()
    
            ' Synchronously read the standard output of the spawned process. 
            Dim reader As StreamReader = process.StandardOutput
            Dim output As String = reader.ReadToEnd()
            Console.WriteLine(output)
    
            process.WaitForExit()
            process.Close()
    
            Console.WriteLine(Environment.NewLine + Environment.NewLine + "Press any key to exit.")
            Console.ReadLine()
        End Sub
    End Module

    COMMAND PROMPT OUTPUT
    11/17/2018 03:32 PM <DIR> Program Files
    01/02/2019 03:33 PM <DIR> Program Files (x86)
    09/12/2015 07:57 PM 0 Recovery.txt
    10/10/2015 08:16 PM <DIR> SolidWorks Admin
    10/10/2015 09:57 PM <DIR> SolidWorks Data
    04/18/2017 06:37 PM <DIR> SymCache
    07/23/2018 06:20 PM <DIR> TEMP
    06/17/2017 02:21 PM <DIR> ti
    04/28/2017 01:26 PM <DIR> Trace
    08/18/2018 05:44 AM <DIR> Users
    05/29/2017 09:21 AM <DIR> VB 2017 Projects
    01/05/2019 11:06 PM <DIR> Windows
    07/16/2017 04:26 PM <DIR> wxWidgets-3.1.0
    1 File(s) 0 bytes
    27 Dir(s) 768,565,334,016 bytes free

    c:\>cd c:\

    c:\>python
    Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 3+9
    12
    >>> print("Hello")
    Hello
    >>>

    VB.NET PROGRAM OUTPUT
    11/17/2018 03:32 PM <DIR> Program Files
    01/02/2019 03:33 PM <DIR> Program Files (x86)
    09/12/2015 07:57 PM 0 Recovery.txt
    10/10/2015 08:16 PM <DIR> SolidWorks Admin
    10/10/2015 09:57 PM <DIR> SolidWorks Data
    04/18/2017 06:37 PM <DIR> SymCache
    07/23/2018 06:20 PM <DIR> TEMP
    06/17/2017 02:21 PM <DIR> ti
    04/28/2017 01:26 PM <DIR> Trace
    08/18/2018 05:44 AM <DIR> Users
    05/29/2017 09:21 AM <DIR> VB 2017 Projects
    01/05/2019 11:06 PM <DIR> Windows
    07/16/2017 04:26 PM <DIR> wxWidgets-3.1.0
    1 File(s) 0 bytes
    27 Dir(s) 768,565,334,016 bytes free

    c:\>cd c:\

    c:\>python



  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    731

    Re: Process.StandardOutput not Working with Python Output

    Quote Originally Posted by VB-MCU-User View Post
    Hi, I'm working on a vb.net program that displays the lines from a command prompt. The code is similar to the code below. It works great for all DOS commands that I enter in the command prompt except when I open Python. As you can see in the copy paste that I did below from the command prompt, Python is working fine. However, the program in vb.net doesn't display anything past "c:\python".

    Am I missing something here? Why is the program not displaying the Python output?

    Thank you,

    Robert


    Code:
    Imports System
    Imports System.IO
    Imports System.Diagnostics
    
    Module Module1
        Sub Main()
            Dim process As New Process()
            process.StartInfo.FileName = "cmd.exe"
            process.StartInfo.UseShellExecute = False
            process.StartInfo.RedirectStandardOutput = True
            process.Start()
    
            ' Synchronously read the standard output of the spawned process. 
            Dim reader As StreamReader = process.StandardOutput
            Dim output As String = reader.ReadToEnd()
            Console.WriteLine(output)
    
            process.WaitForExit()
            process.Close()
    
            Console.WriteLine(Environment.NewLine + Environment.NewLine + "Press any key to exit.")
            Console.ReadLine()
        End Sub
    End Module

    COMMAND PROMPT OUTPUT



    VB.NET PROGRAM OUTPUT
    Not sure if this will help, but have you tried redirecting the standarderror stream and reading from there as well?

  3. #3
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    731

    Re: Process.StandardOutput not Working with Python Output

    Double post.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,565

    Re: Process.StandardOutput not Working with Python Output

    Is the Python app outputing to the standard output? that's the first thing that comes to mind. If it's outputing to something other than the standard console, it would affect where the stream goes and would affect the ability for the VB app to pick it up.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,126

    Re: Process.StandardOutput not Working with Python Output

    Hi,

    this isn't my code, found it a while back, so all credits go to Author unknown

    Code:
    Public Class Form1
        Private Results As String
        Private Delegate Sub delUpdate()
        Private Finished As New delUpdate(AddressOf UpdateText)
    
        Private Sub UpdateText()
            TextBox2.Text = Results
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
            CMDThread.Start()
            TextBox1.Text = "tree E:\ChrisCopy  /A /F " '> E:\pdffiles.txt"
    
        End Sub
    
        Private Sub CMDAutomate()
            Dim myprocess As New Process
            Dim StartInfo As New System.Diagnostics.ProcessStartInfo
            StartInfo.FileName = "cmd" 'starts cmd window
            StartInfo.RedirectStandardInput = True
            StartInfo.RedirectStandardOutput = True
            StartInfo.UseShellExecute = False 'required to redirect
            StartInfo.CreateNoWindow = True 'creates no cmd window
            myprocess.StartInfo = StartInfo
            myprocess.Start()
            Dim SR As System.IO.StreamReader = myprocess.StandardOutput
            Dim SW As System.IO.StreamWriter = myprocess.StandardInput
            SW.WriteLine(TextBox1.Text) 'the command you wish to run.....
            SW.WriteLine("exit") 'exits command prompt window
            Results = SR.ReadToEnd 'returns results of the command window
            SW.Close()
            SR.Close()
            'invokes Finished delegate, which updates textbox with the results text
            Invoke(Finished)
        End Sub
    End Class
    HTH
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    97

    Re: Process.StandardOutput not Working with Python Output

    Peter,

    Yes, I tried reading the standarderror after reading your post, but that didn't help much. I have a feeling this issue has something to do with the encoding. I have tried different encodings, but no luck yet.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    97

    Re: Process.StandardOutput not Working with Python Output

    techgnome,

    Yes, Python is outputing to the standard output. It's weird because the console output can see the version of Python, but cannot execute it.

    Code:
    c:\>python -V
    Python 3.7.2
    
    c:\>

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    97

    Re: Process.StandardOutput not Working with Python Output

    Well, after banging my head against the walls trying to make this work, I settled with running the whole Python command from the command prompt instead of running the commands from inside Python. It works!! It looks something like in the output below. Apparently, once that you enter the Python shell the Process.StandardOutput property cannot read the output.

    Code:
    c:\>python -c "import sys; print(3+4)"
    7
    
    c:\>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width