Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

cin.get == Console::?

This will hopefully be a quick and simple one.
I'm using VS.NET to write C++ code now, having originally learned on VS 6 and gcc.

I'm writing a .NET Console app, and trying to achieve the simple "press any key to continue" functionality.
I previously used cin.get, and I'm wondering if there's a way to do the same thing with the .NET libraries.

Console::Read waits for a return character, so that's no good.
Any suggestions?

Geoff
Tuesday, March 16, 2004

The console support in .NET is not on the level you need.

To achieve what you want, you're going to need to interop to the Win32 console APIs.

Brad Wilson (dotnetguy.techieswithcats.com)
Tuesday, March 16, 2004

Here is one way to do this using interop with the Win32 functions:

using System;

public class Test
{
    public static void Main()
    {
        TurnOffLineInputMode();
        Console.WriteLine("Press any key to continue.");
        Console.Read();
    }

    [System.Runtime.InteropServices.DllImport("kernel32")]
    private static extern int SetConsoleMode(IntPtr hConsoleHandle, int dwMode);

    [System.Runtime.InteropServices.DllImport("kernel32")]
    private static extern int GetConsoleMode(IntPtr hConsoleHandle, ref int dwMode);

    public static void TurnOffLineInputMode()
    {
        const int ENABLE_LINE_INPUT = 0x2;
        const int ENABLE_ECHO_INPUT = 0x4;
        const int CONIN = 3;

        int ConsoleMode = 0;

        IntPtr StdIn = new IntPtr(CONIN);
        GetConsoleMode(StdIn, ref ConsoleMode);

        ConsoleMode = (ConsoleMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
        SetConsoleMode(StdIn, ConsoleMode);

    }
}

Mike Treit
Wednesday, March 17, 2004

You might want to check this article from MSDN magazine a few months back:

http://msdn.microsoft.com/msdnmag/issues/04/02/NETConsoleApps/

Michael Joyner
Thursday, March 18, 2004

*  Recent Topics

*  Fog Creek Home