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.

129 lines
2.4 KiB

  1. // Copyright (c) 2000 Microsoft Corporation
  2. //
  3. // Wrappers of wincrui.h APIs
  4. //
  5. // 19 July 2000 sburns
  6. #include "headers.hxx"
  7. #include "CredentialUiHelpers.hpp"
  8. String
  9. CredUi::GetUsername(HWND credControl)
  10. {
  11. // LOG_FUNCTION(CredUi::GetUsername);
  12. ASSERT(Win::IsWindow(credControl));
  13. String result;
  14. size_t length = Credential_GetUserNameLength(credControl);
  15. if (length)
  16. {
  17. result.resize(length + 1, 0);
  18. BOOL succeeded =
  19. Credential_GetUserName(credControl,
  20. const_cast<WCHAR*>(result.c_str()),
  21. length);
  22. ASSERT(succeeded);
  23. if (succeeded)
  24. {
  25. result.resize(wcslen(result.c_str()));
  26. }
  27. else
  28. {
  29. result.erase();
  30. }
  31. }
  32. // LOG(result);
  33. return result;
  34. }
  35. EncodedString
  36. CredUi::GetPassword(HWND credControl)
  37. {
  38. LOG_FUNCTION(CredUi::GetPassword);
  39. ASSERT(Win::IsWindow(credControl));
  40. EncodedString result;
  41. // add 1 for super-paranoid null terminator.
  42. size_t length = Credential_GetPasswordLength(credControl) + 1;
  43. if (length)
  44. {
  45. WCHAR* cleartext = new WCHAR[length];
  46. ::ZeroMemory(cleartext, sizeof(WCHAR) * length);
  47. BOOL succeeded =
  48. Credential_GetPassword(
  49. credControl,
  50. cleartext,
  51. length - 1);
  52. ASSERT(succeeded);
  53. result.Encode(cleartext);
  54. // make sure we scribble out the cleartext.
  55. ::ZeroMemory(cleartext, sizeof(WCHAR) * length);
  56. delete[] cleartext;
  57. }
  58. // don't log the password...
  59. return result;
  60. }
  61. HRESULT
  62. CredUi::SetUsername(HWND credControl, const String& username)
  63. {
  64. LOG_FUNCTION(CredUi::SetUsername);
  65. ASSERT(Win::IsWindow(credControl));
  66. HRESULT hr = S_OK;
  67. // username may be empty
  68. BOOL succeeded = Credential_SetUserName(credControl, username.c_str());
  69. ASSERT(succeeded);
  70. // BUGBUG what if it failed? Is GetLastError valid?
  71. return hr;
  72. }
  73. HRESULT
  74. CredUi::SetPassword(HWND credControl, const EncodedString& password)
  75. {
  76. LOG_FUNCTION(CredUi::SetPassword);
  77. ASSERT(Win::IsWindow(credControl));
  78. HRESULT hr = S_OK;
  79. // password may be empty
  80. WCHAR* cleartext = password.GetDecodedCopy();
  81. BOOL succeeded = Credential_SetPassword(credControl, cleartext);
  82. ASSERT(succeeded);
  83. ::ZeroMemory(cleartext, sizeof(WCHAR) * password.GetLength());
  84. delete[] cleartext;
  85. // BUGBUG what if it failed? Is GetLastError valid?
  86. return hr;
  87. }