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.

127 lines
2.7 KiB

  1. //
  2. // Registry.cpp
  3. //
  4. #include "stdafx.h"
  5. #include "Registry.h"
  6. CRegistry::CRegistry()
  7. {
  8. m_hkey = NULL;
  9. bhkeyValid = FALSE;
  10. }
  11. CRegistry::~CRegistry()
  12. {
  13. Close();
  14. }
  15. CRegistry::CRegistry(const TCHAR *pszSubKey, HKEY hkey)
  16. {
  17. m_hkey = NULL;
  18. bhkeyValid = FALSE;
  19. Open(pszSubKey, hkey);
  20. }
  21. BOOL CRegistry::Open(const TCHAR *pszSubKey, HKEY hkey)
  22. {
  23. Close();
  24. m_error = RegCreateKey(hkey, pszSubKey, &m_hkey);
  25. if (m_error)
  26. bhkeyValid = FALSE;
  27. else
  28. bhkeyValid = TRUE;
  29. return bhkeyValid;
  30. }
  31. BOOL CRegistry::Close()
  32. {
  33. if (bhkeyValid)
  34. RegCloseKey(m_hkey);
  35. m_hkey = NULL;
  36. bhkeyValid = FALSE;
  37. return TRUE;
  38. }
  39. BOOL CRegistry::CreateKey(const TCHAR *pszSubKey)
  40. {
  41. HKEY hKey;
  42. m_error = RegCreateKey(m_hkey, pszSubKey, &hKey);
  43. if(m_error)
  44. return FALSE;
  45. return TRUE;
  46. }
  47. BOOL CRegistry::DeleteKey(const TCHAR *pszSubKey)
  48. {
  49. m_error = RegDeleteKey(m_hkey,pszSubKey);
  50. if(m_error)
  51. return FALSE;
  52. else
  53. return TRUE;
  54. }
  55. LONG CRegistry::SetValue(const TCHAR *pszValue, DWORD dwNumber)
  56. {
  57. if (bhkeyValid) {
  58. m_error = RegSetValueEx(m_hkey,
  59. pszValue,
  60. 0,
  61. REG_DWORD,
  62. (BYTE *)&dwNumber,
  63. sizeof(dwNumber));
  64. }
  65. return m_error;
  66. }
  67. LONG CRegistry::GetValue(const TCHAR *pszValue, DWORD dwDefault)
  68. {
  69. DWORD dwType = REG_DWORD;
  70. long dwNumber = 0L;
  71. DWORD dwSize = sizeof(dwNumber);
  72. if (bhkeyValid) {
  73. m_error = RegQueryValueEx(m_hkey,
  74. (LPTSTR) pszValue,
  75. 0,
  76. &dwType,
  77. (LPBYTE)&dwNumber,
  78. &dwSize);
  79. }
  80. if (m_error)
  81. dwNumber = dwDefault;
  82. return dwNumber;
  83. }
  84. VOID CRegistry::MoveToSubKey(const TCHAR *pszSubKeyName)
  85. {
  86. HKEY _hNewKey;
  87. if (bhkeyValid) {
  88. m_error = RegOpenKey ( m_hkey,
  89. pszSubKeyName,
  90. &_hNewKey );
  91. if (m_error == ERROR_SUCCESS) {
  92. RegCloseKey(m_hkey);
  93. m_hkey = _hNewKey;
  94. }
  95. }
  96. }
  97. LONG CRegistry::EnumerateKeys(DWORD dwIndex,TCHAR *pszKeyName, DWORD dwSize)
  98. {
  99. FILETIME ft;
  100. LONG lError = 0;
  101. memset(pszKeyName,0,sizeof(pszKeyName));
  102. lError = RegEnumKeyEx(m_hkey, // handle to key to enumerate
  103. dwIndex, // index of subkey to enumerate
  104. pszKeyName, // address of buffer for subkey name
  105. &dwSize, // address for size of subkey buffer
  106. NULL, // reserved
  107. NULL, // address of buffer for class string
  108. NULL, // address for size of class buffer
  109. &ft // address for time key last written to
  110. );
  111. return lError;
  112. }