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.

186 lines
4.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 1998.
  5. //
  6. // File: regauto.hxx
  7. //
  8. // Contents: CRegAutomatic class
  9. // CRegAutoStringValue
  10. // CRegAutoDWORDValue
  11. //
  12. // History: 23 Oct 97 AlanW Created
  13. // 14 Aug 98 KLam Inlined methods
  14. //
  15. //--------------------------------------------------------------------------
  16. #pragma once
  17. #include <regacc.hxx>
  18. #include <regevent.hxx>
  19. //+-------------------------------------------------------------------------
  20. //
  21. // Class: CRegAutomatic
  22. //
  23. // Purpose: A base class for an automatically updating registry value(s)
  24. //
  25. // History: 23 Oct 97 AlanW Created
  26. // 14 Aug 98 KLam Inlined CheckIfUpdated
  27. //
  28. //--------------------------------------------------------------------------
  29. class CRegAutomatic : protected CRegChangeEvent
  30. {
  31. public:
  32. CRegAutomatic( const WCHAR * wcsRegKey ) :
  33. CRegChangeEvent( wcsRegKey ),
  34. _mutex( )
  35. {
  36. }
  37. ~CRegAutomatic()
  38. {
  39. }
  40. // check if reg event fired. If so, call derived ReadRegValues().
  41. BOOL CheckIfUpdated( )
  42. {
  43. CLock lock( _mutex );
  44. ULONG res = WaitForSingleObject( GetEventHandle(), 0 );
  45. if ( WAIT_OBJECT_0 == res )
  46. {
  47. ReadRegValues();
  48. Reset();
  49. }
  50. return (WAIT_OBJECT_0 == res);
  51. }
  52. virtual void ReadRegValues() = 0;
  53. protected:
  54. CMutexSem _mutex;
  55. };
  56. //+-------------------------------------------------------------------------
  57. //
  58. // Class: CRegAutoStringValue
  59. //
  60. // Purpose: An automatically updating registry value of type REG_SZ
  61. //
  62. // Notes: The value is assumed to fit in MAX_PATH characters.
  63. // Careful casting away const since the class maintains
  64. // pointers to the strings passed into the constructor.
  65. //
  66. // History: 23 Oct 97 AlanW Created
  67. // 14 Aug 98 KLam Changed inheritance to public to allow
  68. // access to CheckIfUpdated
  69. // Inlined ReadRegValues
  70. //
  71. //--------------------------------------------------------------------------
  72. class CRegAutoStringValue : public CRegAutomatic
  73. {
  74. public:
  75. CRegAutoStringValue( const WCHAR * wcsRegKey,
  76. const WCHAR * wcsRegValueName,
  77. const WCHAR * wcsDefaultValue = 0 ) :
  78. CRegAutomatic( wcsRegKey ),
  79. _pwszRegValueName( wcsRegValueName ),
  80. _pwszDefaultValue( wcsDefaultValue )
  81. {
  82. ReadRegValues();
  83. }
  84. ~CRegAutoStringValue()
  85. {
  86. }
  87. const ULONG Get( WCHAR *pwc, ULONG cch )
  88. {
  89. CheckIfUpdated();
  90. CLock lock( _mutex );
  91. // copy the whole string under lock
  92. ULONG cchBuf = wcslen( _awchValue );
  93. if ( cch > cchBuf )
  94. wcscpy( pwc, _awchValue );
  95. return cchBuf;
  96. }
  97. void ReadRegValues()
  98. {
  99. CRegAccess reg( RTL_REGISTRY_ABSOLUTE, GetKeyName() );
  100. XPtrST<WCHAR> xwszValue( reg.Read( _pwszRegValueName, _pwszDefaultValue ) );
  101. wcsncpy( _awchValue, xwszValue.GetPointer(), MAX_PATH );
  102. }
  103. private:
  104. const WCHAR * _pwszRegValueName;
  105. const WCHAR * _pwszDefaultValue;
  106. WCHAR _awchValue[MAX_PATH];
  107. };
  108. //+-------------------------------------------------------------------------
  109. //
  110. // Class: CRegAutoDWORDValue
  111. //
  112. // Purpose: An automatically updating registry value of type REG_DWORD
  113. //
  114. // Notes: Careful casting away const since the class maintains
  115. // pointers to the strings passed into the constructor.
  116. //
  117. // History: 13 Aug 98 KLam Created
  118. //
  119. //--------------------------------------------------------------------------
  120. class CRegAutoDWORDValue : public CRegAutomatic
  121. {
  122. public:
  123. CRegAutoDWORDValue( const WCHAR * wcsRegKey,
  124. const WCHAR * wcsRegValueName,
  125. DWORD dwDefaultValue ) :
  126. CRegAutomatic( wcsRegKey ),
  127. _pwszRegValueName( wcsRegValueName ),
  128. _dwDefaultValue( dwDefaultValue )
  129. {
  130. ReadRegValues();
  131. }
  132. ~CRegAutoDWORDValue()
  133. {
  134. }
  135. BOOL Get ( DWORD *pdw )
  136. {
  137. BOOL fUpdated = CheckIfUpdated();
  138. InterlockedExchange ( (long *)pdw, _dwValue );
  139. return fUpdated;
  140. }
  141. void ReadRegValues()
  142. {
  143. CRegAccess reg( RTL_REGISTRY_ABSOLUTE, GetKeyName() );
  144. _dwValue = reg.Read ( _pwszRegValueName, _dwDefaultValue );
  145. }
  146. private:
  147. const WCHAR * _pwszRegValueName;
  148. DWORD _dwDefaultValue;
  149. DWORD _dwValue;
  150. };