// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; namespace MyFormProject { class MainForm : Form { public MainForm() { InitializeComponent(); } void InitializeComponent() { SuspendLayout(); Name = "MainForm"; Text = "This is my form"; Size = new Size(300, 300); ResumeLayout(false); } private const uint STD_INPUT_HANDLE = 0xfffffff6; private const uint STD_OUTPUT_HANDLE = 0xfffffff5; private const uint STD_ERROR_HANDLE = 0xfffffff4; [DllImport("kernel32.dll")] public static extern bool AllocConsole(); [DllImport("kernel32.dll")] public static extern bool FreeConsole(); [DllImport("kernel32.dll")] public static extern int GetStdHandle(uint nStdHandle); [DllImport("kernel32.dll")] public static extern bool WriteConsole(int hConsoleOutput, string lpBuffer, int nNumberOfCharsToWrite, ref int lpNumberOfCharsWritten, int lpReserved); [DllImport("kernel32.dll")] public static extern bool ReadConsole(int hConsoleInput, StringBuilder lpBuffer, int nNumberOfCharsToRead, ref int lpNumberOfCharsRead, int lpReserved); private static int stdin; private static int stdout; private static void WriteLine(string s) { int len = 0; WriteConsole(stdout, s + "\r\n", s.Length + 2, ref len, 0); } private static string ReadLine() { int len = 0; StringBuilder sb = new StringBuilder(); ReadConsole(stdin, sb, 256, ref len, 0); return sb.ToString(0, sb.Length - 2); } [STAThread] public static void Main(string[] args) { //Application.Run(new MainForm()); AllocConsole(); stdin = GetStdHandle(STD_INPUT_HANDLE); stdout = GetStdHandle(STD_OUTPUT_HANDLE); WriteLine("Enter line below"); string s = ReadLine(); WriteLine(s); WriteLine("Press enter to exit"); ReadLine(); FreeConsole(); } } }