Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.6 KiB

  1. // Password.cpp : Implementation of CPassword
  2. #include "stdafx.h"
  3. #include "ScriptPW.h"
  4. #include "Password.h"
  5. #include <conio.h>
  6. #define MAX_PASSWORD_SIZE 256
  7. #define CARRIAGE_RETURN 0x0D
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CPassword
  10. STDMETHODIMP CPassword::GetPassword(BSTR *bstrOutPassword)
  11. {
  12. // TODO: Add your implementation code here
  13. HANDLE hConsoleInput;
  14. TCHAR *tstrPassword;
  15. TCHAR wch;
  16. int i=0;
  17. DWORD nNumberOfCharsToRead=1;
  18. DWORD dwNumberOfCharsRead;
  19. DWORD dwPrevConsoleMode;
  20. hConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
  21. GetConsoleMode(hConsoleInput, &dwPrevConsoleMode);
  22. if(!SetConsoleMode(hConsoleInput,ENABLE_PROCESSED_INPUT))
  23. return E_FAIL;
  24. tstrPassword = (TCHAR *)malloc(MAX_PASSWORD_SIZE * sizeof(TCHAR));
  25. if(tstrPassword == NULL)
  26. return E_FAIL;
  27. while(1)
  28. {
  29. if(!ReadConsole(hConsoleInput, // handle to console input buffer
  30. &wch, // data buffer
  31. nNumberOfCharsToRead, // number of characters to read
  32. &dwNumberOfCharsRead, // number of characters read
  33. NULL))
  34. {
  35. //Set the original console settings
  36. SetConsoleMode(hConsoleInput, dwPrevConsoleMode);
  37. //Free the memory
  38. if(tstrPassword)
  39. free(tstrPassword);
  40. return E_FAIL;
  41. }
  42. if(wch == CARRIAGE_RETURN)
  43. break;
  44. *(tstrPassword+i) = wch;
  45. i++;
  46. if(i == MAX_PASSWORD_SIZE)
  47. break;
  48. }
  49. *(tstrPassword+i) = _T('\0');
  50. CComBSTR bstrPassword(tstrPassword);
  51. *bstrOutPassword = bstrPassword.Copy();
  52. //Set the original console settings
  53. SetConsoleMode(hConsoleInput, dwPrevConsoleMode);
  54. //Free the memory
  55. if(tstrPassword)
  56. free(tstrPassword);
  57. return S_OK;
  58. }