views:

44

answers:

1

Currently I am using this implementation to hide user input during password entry:

void setEcho(bool enable) {
  HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
  DWORD mode;
  GetConsoleMode(hStdin, &mode);

  if(enable) {
    mode |= ENABLE_ECHO_INPUT;
  } else {
    mode &= ~ENABLE_ECHO_INPUT;
  }

  SetConsoleMode(hStdin, mode);
}

The user needs to be able to have positive feed back that text entry is being made. What techniques are available in a Win32 environment using C++?

A: 

The solution is to not use stdio.h but to use conio.h. The following routine solves the above problem. Note that disabling echo is no longer needed:

void scanPass(char* passwordEntry, int length) {
  int index, ch;

  for(index = 0; index < (length - 1) && ((ch = _getch()) != EOF)
                                      && (ch != 13); index++) {
    passwordEntry[index] = (char)ch;
    _putch('*');
  }

  passwordEntry[index] = '\0';
}

The answer in this case is to use the correct tool for the job. (And to know of the right tool.)

Erick