0

That's what I want to do with C#. I have two buttons and two textboxes, and first I want to write something in textbox1, and this message is redirected to the console, and at this time, when something new appears in my console, textbox2 needs to read it, and the process should continue and continue. I want to use my simple DLL library, which works in this scenario. I know how to write messages to the console, but I can not read every new one from the console and write it to textbox2.

This is my redirecting from textbox into console, now i need to read anything new appearing in the console to my textbox2.

public partial class NativeMethods
{
    public static Int32 STD_OUTPUT_HANDLE = -11;

    [System.Runtime.InteropServices.DllImport("kernel32.dll")]

    public static extern System.IntPtr GetStdHandle(Int32 nStdHandle);

    [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "AllocConsole")]
    [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]

    public static extern bool AllocConsole();
}

And somewhere else....

public void console()
{
    if(NativeMethods.AllocConsole())
    {
        IntPtr handle = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE);

        Console.WriteLine("type: ");
    }
    else
    {
        Console.WriteLine("Waiting...");
    }
}

Using these when a button is clicked.

iBug
  • 35,554
  • 7
  • 89
  • 134
Rasuljon
  • 35
  • 2
  • 1
    Your question was a duplicate [the last time you posted it](https://stackoverflow.com/questions/48132347/redirection-of-input-console-into-textbox), and it remains a duplicate now. Also, it is against Stack Overflow policy to create new user accounts just to bypass user restrictions. – Peter Duniho Jan 07 '18 at 05:17

1 Answers1

-1

If you can start the console programm from your Application, it is a simple mater of redirecting the I/O Streams on the Process class:

If the process is started by anybody else - whatever approach you will find is basically a hack. So you will very likely have to go to unamanged code, wich negates teh point of doing this in .NET to begin with. Plus it might trigger security features.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • _"[**AllocConsole** - This function is primarily used by graphical user interface (GUI) application to create a console window. GUI applications are initialized without a console.](https://learn.microsoft.com/en-us/windows/console/allocconsole)"_ –  Jan 07 '18 at 05:17
  • 1
    Also, your answer is essentially a link only answer –  Jan 07 '18 at 05:23