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.

189 lines
4.1 KiB

  1. #define STRICT
  2. #include "mcReg.h"
  3. //---------------------------------------------------------------------------
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9. //---------------------------------------------------------------------------
  10. // ctor
  11. //---------------------------------------------------------------------------
  12. CMcRegistry::CMcRegistry()
  13. : m_hkey(NULL)
  14. {
  15. }
  16. //---------------------------------------------------------------------------
  17. // dtor
  18. //---------------------------------------------------------------------------
  19. CMcRegistry::~CMcRegistry()
  20. {
  21. if (m_hkey)
  22. {
  23. CloseKey();
  24. }
  25. }
  26. //---------------------------------------------------------------------------
  27. // OpenKey
  28. //---------------------------------------------------------------------------
  29. bool
  30. CMcRegistry::OpenKey(
  31. HKEY hkeyStart, LPCTSTR strKey, REGSAM sam /* = KEY_READ | KEY_WRITE */)
  32. {
  33. long lErr = ::RegOpenKeyEx(hkeyStart, strKey, 0, sam, &m_hkey);
  34. if (ERROR_SUCCESS != lErr)
  35. {
  36. m_hkey = NULL;
  37. }
  38. return ERROR_SUCCESS == lErr;
  39. }
  40. //---------------------------------------------------------------------------
  41. // CreateKey
  42. //---------------------------------------------------------------------------
  43. bool
  44. CMcRegistry::CreateKey(HKEY hkeyStart, LPCTSTR strKey)
  45. {
  46. // You shouldn't have opened now.
  47. if (m_hkey)
  48. {
  49. _ASSERT(!m_hkey);
  50. return false;
  51. }
  52. long lErr = ::RegCreateKey(hkeyStart, strKey, &m_hkey);
  53. if (ERROR_SUCCESS != lErr)
  54. {
  55. m_hkey = NULL;
  56. _ASSERT(false);
  57. }
  58. return ERROR_SUCCESS == lErr;
  59. }
  60. //---------------------------------------------------------------------------
  61. // CloseKey
  62. //---------------------------------------------------------------------------
  63. bool
  64. CMcRegistry::CloseKey()
  65. {
  66. if (!m_hkey)
  67. {
  68. _ASSERT(m_hkey);
  69. return false;
  70. }
  71. long lErr = ::RegCloseKey(m_hkey);
  72. if (ERROR_SUCCESS != lErr)
  73. {
  74. m_hkey = NULL;
  75. _ASSERT(false);
  76. }
  77. return ERROR_SUCCESS == lErr;
  78. }
  79. //---------------------------------------------------------------------------
  80. // GetValue
  81. //---------------------------------------------------------------------------
  82. bool
  83. CMcRegistry::GetValue(LPCTSTR strValue, LPTSTR strData, ULONG nBufferSize)
  84. {
  85. if (!m_hkey)
  86. {
  87. _ASSERT(m_hkey);
  88. return false;
  89. }
  90. DWORD dwType;
  91. ULONG cbData = nBufferSize;
  92. long lErr = ::RegQueryValueEx(
  93. m_hkey, strValue, NULL, &dwType,
  94. reinterpret_cast<PBYTE>(strData), &cbData);
  95. return ERROR_SUCCESS == lErr && REG_SZ == dwType;
  96. }
  97. //---------------------------------------------------------------------------
  98. // GetValue
  99. //---------------------------------------------------------------------------
  100. bool
  101. CMcRegistry::GetValue(LPCTSTR strValue, DWORD& rdw)
  102. {
  103. if (!m_hkey)
  104. {
  105. _ASSERT(m_hkey);
  106. return false;
  107. }
  108. DWORD dwType;
  109. ULONG cbData = sizeof(rdw);
  110. long lErr = ::RegQueryValueEx(
  111. m_hkey, strValue, NULL, &dwType,
  112. reinterpret_cast<PBYTE>(&rdw), &cbData);
  113. return ERROR_SUCCESS == lErr && REG_DWORD == dwType;
  114. }
  115. //---------------------------------------------------------------------------
  116. // SetValue
  117. //---------------------------------------------------------------------------
  118. bool
  119. CMcRegistry::SetValue(LPCTSTR strValue, LPCTSTR strData)
  120. {
  121. if (!m_hkey)
  122. {
  123. _ASSERT(m_hkey);
  124. return false;
  125. }
  126. long lErr = ::RegSetValueEx(
  127. m_hkey, strValue, 0, REG_SZ,
  128. reinterpret_cast<const BYTE*>(strData), sizeof(TCHAR)*(lstrlen(strData) + 1));
  129. if (ERROR_SUCCESS != lErr)
  130. {
  131. _ASSERT(false);
  132. }
  133. return ERROR_SUCCESS == lErr;
  134. }
  135. //---------------------------------------------------------------------------
  136. // SetValue
  137. //---------------------------------------------------------------------------
  138. bool
  139. CMcRegistry::SetValue(LPCTSTR strValue, DWORD rdw)
  140. {
  141. if (!m_hkey)
  142. {
  143. _ASSERT(m_hkey);
  144. return false;
  145. }
  146. long lErr = ::RegSetValueEx(
  147. m_hkey, strValue, 0, REG_DWORD,
  148. reinterpret_cast<PBYTE>(&rdw), sizeof(rdw));
  149. if (ERROR_SUCCESS != lErr)
  150. {
  151. _ASSERT(false);
  152. }
  153. return ERROR_SUCCESS == lErr;
  154. }