Leaked source code of windows server 2003
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.

111 lines
2.4 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. username.hxx
  5. Abstract:
  6. Class for managing user name
  7. Author:
  8. Richard L Firth (rfirth) 30-Aug-1997
  9. Revision History:
  10. 30-Aug-1997 rfirth
  11. Created
  12. --*/
  13. class CUserName {
  14. private:
  15. CRITICAL_SECTION m_CritSec;
  16. ICSTRING m_UserName;
  17. BOOL m_fInitialized;
  18. public:
  19. CUserName() {
  20. InitializeCriticalSection(&m_CritSec);
  21. m_UserName = NULL;
  22. m_fInitialized = TRUE;
  23. }
  24. ~CUserName() {
  25. if (m_fInitialized) {
  26. // CRT may result in the destructor being called twice.
  27. // Prevent the delete critsec from happening a second time.
  28. DeleteCriticalSection(&m_CritSec);
  29. m_fInitialized = FALSE;
  30. }
  31. }
  32. VOID Set(VOID) {
  33. EnterCriticalSection(&m_CritSec);
  34. if (!m_UserName.HaveString()) {
  35. char buf[256];
  36. DWORD length = sizeof(buf);
  37. if (GetUserName(buf, &length)) {
  38. m_UserName = buf;
  39. }
  40. }
  41. LeaveCriticalSection(&m_CritSec);
  42. }
  43. LPSTR Get(LPSTR lpszBuffer, DWORD dwLength) {
  44. EnterCriticalSection(&m_CritSec);
  45. if (!m_UserName.HaveString()) {
  46. Set();
  47. }
  48. if ((m_UserName.StringLength() < (int)dwLength) && (dwLength > 0)) {
  49. m_UserName.CopyTo(lpszBuffer, dwLength);
  50. } else {
  51. if (dwLength > 0) {
  52. *lpszBuffer = '\0';
  53. }
  54. lpszBuffer = "";
  55. }
  56. LeaveCriticalSection(&m_CritSec);
  57. return lpszBuffer;
  58. }
  59. LPSTR Get(LPSTR lpszBuffer, LPDWORD lpdwLength) {
  60. EnterCriticalSection(&m_CritSec);
  61. if (!m_UserName.HaveString()) {
  62. Set();
  63. }
  64. DWORD dwLength = *lpdwLength;
  65. if ((m_UserName.StringLength() < (int)dwLength) && (dwLength != 0)) {
  66. m_UserName.CopyTo(lpszBuffer, lpdwLength);
  67. } else {
  68. if (dwLength > 0) {
  69. *lpszBuffer = '\0';
  70. }
  71. lpszBuffer = "";
  72. *lpdwLength = 0;
  73. }
  74. LeaveCriticalSection(&m_CritSec);
  75. return lpszBuffer;
  76. }
  77. VOID Clear(VOID) {
  78. EnterCriticalSection(&m_CritSec);
  79. m_UserName = NULL;
  80. LeaveCriticalSection(&m_CritSec);
  81. }
  82. };