-2

I've seen a C# example using ConsoleRead API function but when I've tried to translate it to VBNET I get a lots of errors, also in other sites like pinvoke the unique example is for C# too, I can't find any good information for VBNET of ConsoleRead API function (if exist a way to read the console buffer without APIS then I get no idea).

Also I've tried this console buffer reader Class for VBNET (http://pastebin.com/XYxakDTV) but it throws an unhandled exception with message like this "Controller not valid".

Someone could illustrate me and all other people with an example for VBNET of how to launch a process from a GUI app (WindowsForm) to read the console output to retrieve characters/strings?

UPDATE:

I'm not sure but I think that a launched process (using System.Process Class) does not assign the console to the app so I think that all the examples seen here in MSDN could not help me: http://msdn.microsoft.com/en-us/library/system.console.aspx

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • How do you start the process? I would've expected that you could do this with stream redirection, without *needing* P/Invoke, after all... – Rowland Shaw Oct 07 '13 at 15:48
  • @Rowland Shaw Hi, I start the process as normally, the process has their own progressbar and while the progress is not fullfilled by the process I can't read the entire line (the CLI progressbar line), so I need to read character by character the console output to retrieve the progress, I've solved that problem reading char by char using a stream reader, but I have another problem unresolved, the process error output can't be read (at least I've tried much things but can't read it) so I hope if reading it from console buffer I would be able to read any of the process outputs (except input) – ElektroStudios Oct 07 '13 at 15:52

1 Answers1

1

Use

StandardOutput.Read

I think that the answer is the same as for your other question Run a commandline process and get the output while that process still running?

Edit you are asking for an form application to read console output. So let's have a console application ():

Module Module1

    Sub Main()
        While True
            ' data = Console.ReadKey.KeyChar
            Dim Generator As System.Random = New System.Random()
            Console.Write(Generator.Next(0, 100) & " ")
            Threading.Thread.Sleep(1000)
        End While
    End Sub

End Module 

It generates a space separated numbers one per second. Now the forms application. Let's have a form with a multiline TextBox named txtResult and a button cmdStart :

Private Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click
        Dim ProcInfo As New ProcessStartInfo With
            {.FileName = "DataApp.exe", .RedirectStandardOutput = True, .UseShellExecute = False}
        Dim proc As Process = Process.Start(ProcInfo)

        While Not proc.HasExited
            Dim a As String = ChrW(proc.StandardOutput.Read)
            If a = " " Then
                txtResult.Text &= vbCrLf
            Else
                txtResult.Text &= a
            End If
            Threading.Thread.Sleep(100)
            Application.DoEvents()
        End While

    End Sub

It writes the numbers to the TextBox one per a line. There is no API magic but it works.

Community
  • 1
  • 1
IvanH
  • 5,039
  • 14
  • 60
  • 81
  • 1
    Hi, of course with that I can read the output but not the buffer, also the "Read" method is for read one one per one character, it returns only one integer value which needs to be converted to a character so the code using a string variable in the other answer is totally wrong because the output will be a lot of numebrs, not characters...anyways I know how to do that but is not what I've asked for, how to read the buffer (where I suppose all outputs are merged in that buffer),but really thankyou anyways.Forgive my English. – ElektroStudios Oct 10 '13 at 17:18
  • 2
    @ElektroHacker You are writing about reading a buffer it means serialized data. Necessity to parse numbers from strings is annoying but solvable and not too difficult. – IvanH Oct 11 '13 at 13:53