[Vb.net]How can I redirect standard Output to stream then use it?

Discussion in 'Mixed Languages' started by flare4000, Dec 27, 2010.

  1. flare4000

    flare4000 MDL Senior Member

    Apr 23, 2010
    414
    111
    10
    #1 flare4000, Dec 27, 2010
    Last edited by a moderator: Apr 20, 2017
    Okay I am trying to use slmgr -dlv to find if kms activation was successful but can't get the output so here is what I got

    Code:
     Public Sub checks()
            Dim attempts As Single = 1
            Dim p As New Process()
            Dim sw As StreamWriter
            Dim sr As StreamReader
            Dim err As StreamReader
            p.StartInfo.UseShellExecute = False
            p.StartInfo.RedirectStandardInput = True
            p.StartInfo.RedirectStandardOutput = True
            p.StartInfo.RedirectStandardError = True
            p.StartInfo.CreateNoWindow = True
            p.StartInfo.FileName = Temp & "/system32/cscript.exe"
            p.StartInfo.Arguments = Temp & "\slmgr.vbs -dli"
            p.Start()
            sw = p.StandardInput
            sr = p.StandardOutput
            sw.AutoFlush = True
            Dim cmd = Temp & "\slmgr.vbs -dli"
            sw.WriteLine(cmd)
            TextBox1.Text = TextBox1.Text & sr.ReadToEnd()
            TextBox1.Text += TextBox1.Text & err.ReadToEnd()
            If InStr(TextBox4.Text, "259200") Then
                Kill32()
                Kill64()
                Delete()
                TextBox4.Text = "Activation Was Succesful!"
                Done()
            Else
                If attempts = 4 Then
                    MessageBox.Show("Could not activate please go to support")
                    Kill32()
                    Kill64()
                    Delete()
                    Done()
                End If
                attempts = attempts + 1
                Kmsact()
                checks()
            End If
        End Sub
    And it always gives me UseShellExecute must be set to False to redirect output...

    Thanks,

    Flare4000
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    #2 Calistoga, Dec 28, 2010
    Last edited by a moderator: Apr 20, 2017
    Does it work if you use this method as a starting point for modification? I've had success with it earlier.
    Code:
    void RunWithRedirect(string cmdPath)
    {
        var proc = new Process();
        proc.StartInfo.FileName = cmdPath;
    
        // set up output redirection
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;    
        proc.EnableRaisingEvents = true;
        proc.StartInfo.CreateNoWindow = true;
        // see below for output handler
        proc.ErrorDataReceived += proc_DataReceived;
        proc.OutputDataReceived += proc_DataReceived;
    
        proc.Start();
    
        proc.BeginErrorReadLine();
        proc.BeginOutputReadLine();
    
        proc.WaitForExit();
    }
    
    void proc_DataReceived(object sender, DataReceivedEventArgs e)
    {
        // output will be in string e.Data
        // modify TextBox.Text here
    }
    
    Source

    The method demonstrated above takes advantage of event handlers to accomplish its mission.
    Code:
    proc.ErrorDataReceived += proc_DataReceived;
    proc.OutputDataReceived += proc_DataReceived;
    
     
  3. flare4000

    flare4000 MDL Senior Member

    Apr 23, 2010
    414
    111
    10
    #3 flare4000, Dec 28, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Thanks got it to work here is what I had done
    Code:
        Public Sub checks()
            Dim attempts As Single = 0
            Dim proc = New Process()
            proc.StartInfo.FileName = Temp & "/system32/cscript.exe"
            proc.StartInfo.Arguments = Temp & "\slmgr.vbs -dli"
            ' set up output redirection
            proc.StartInfo.RedirectStandardOutput = True
            proc.StartInfo.RedirectStandardError = True
            proc.EnableRaisingEvents = True
            proc.StartInfo.CreateNoWindow = True
            proc.StartInfo.UseShellExecute = False
            ' see below for output handler
            AddHandler proc.ErrorDataReceived, AddressOf proc_DataReceived
            AddHandler proc.OutputDataReceived, AddressOf proc_DataReceived
            proc.Start()
            proc.BeginErrorReadLine()
            proc.BeginOutputReadLine()
            proc.WaitForExit()
            If InStr(TextBox4.Text, "259200") Then
                Kill32()
                Kill64()
                Delete()
                TextBox4.Text = "Activation Was Succesful!"
                Done()
            Else
                If attempts = 4 Then
                    MessageBox.Show("Could not activate please go to support")
                    Kill32()
                    Kill64()
                    Delete()
                    Done()
                End If
                attempts = attempts + 1
                Kmsact()
                checks()
            End If
        End Sub
        Private Sub proc_DataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
            ' output will be in string e.Data
            ' modify TextBox.Text here
            TextBox4.Text = TextBox4.Text & e.Data
        End Sub
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...