Re: non blocking console read

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Richard Bell (rbell01824_at_earthlink.net)
Date: 03/04/04


Date: Thu, 04 Mar 2004 15:27:21 GMT

Thanks Tom.

With your clue and a quick Google I found a great example.

Regards,
Richard

public class PwdConsole {
   [DllImport("kernel32", SetLastError=true)]
   static extern IntPtr GetStdHandle(IntPtr whichHandle);
   [DllImport("kernel32", SetLastError=true)]
   static extern bool GetConsoleMode(IntPtr handle, out uint mode);
   [DllImport("kernel32", SetLastError=true)]
   static extern bool SetConsoleMode(IntPtr handle, uint mode);

   static readonly IntPtr STD_INPUT_HANDLE = new IntPtr(-10);
   const int ENABLE_LINE_INPUT = 2;
   const uint ENABLE_ECHO_INPUT = 4;

   public static string ReadLine() {
      // turn off console echo
      IntPtr hConsole = GetStdHandle(STD_INPUT_HANDLE);
      uint oldMode;
      if (!GetConsoleMode(hConsole, out oldMode)) {
         throw new ApplicationException("GetConsoleMode failed");
      }
      uint newMode = oldMode & ~(ENABLE_LINE_INPUT |
ENABLE_ECHO_INPUT);
      if (!SetConsoleMode(hConsole, newMode)) {
         throw new ApplicationException("SetConsoleMode failed");
      }
      int i;
      StringBuilder secret = new StringBuilder();
      while (true) {
         i = Console.Read ();
         if (i == 13) // break when
            break;
         secret.Append((char) i);
         Console.Write ("*");
      }

      Console.WriteLine();
      // restore console echo and line input mode
      if (!SetConsoleMode(hConsole, oldMode)) {
         throw new ApplicationException("SetConsoleMode failed");
      }
      return secret.ToString();
   }
}

On Wed, 03 Mar 2004 21:37:12 -0800, Tom Shelton <tom@mtogden.com>
wrote:

>On 2004-03-03, Richard Bell <rbell01824@earthlink.net> wrote:
>> Is there a way to do a non-blocking console read in VB.net? Something
>> a bit like the old INKEY$?
>
>You can use P/Invoke to call the SetConsoleMode API to change the input
>modes...



Relevant Pages

  • Re: Synchronizing processes in C#
    ... static extern IntPtr CreateEvent(IntPtr lpEventAttributes, ... static extern bool ResetEvent; ... static bool CreateManagedEvent(out IntPtr handle,out bool createdNew) ... > In process 1 I tried using a Mutex: ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Get Whether System is In Insert Mode
    ... static extern bool AttachThreadInput(int idAttach, int idAttachTo,bool ... many apps such as VS.net IDE etc ignore this and simply toggle an ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: can directsound work in a webservice
    ... static extern IntPtr GetThreadDesktop; ... static extern uint GetCurrentThreadId; ... static extern bool SetThreadDesktop; ... static extern IntPtr OpenInputDesktop(uint dwFlags, bool fInherit, ...
    (microsoft.public.win32.programmer.directx.audio)
  • Re: Character encoding problems reading Html from Clipboard
    ... static extern IntPtr GetClipboardData; ... static extern bool OpenClipboard; ... static extern bool CloseClipboard(); ... static extern IntPtr GlobalLock(IntPtr hMem); ...
    (microsoft.public.dotnet.framework.windowsforms)