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.

144 lines
3.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1997
  6. //
  7. // File: regkey.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <windows.h>
  11. #include "regkey.h"
  12. LONG REGKEY::Close()
  13. {
  14. LONG lRes = ERROR_SUCCESS;
  15. if (m_hKey != NULL)
  16. {
  17. lRes = RegCloseKey(m_hKey);
  18. m_hKey = NULL;
  19. }
  20. return lRes;
  21. }
  22. void REGKEY::Attach(HKEY hKey)
  23. {
  24. assert(m_hKey == NULL);
  25. m_hKey = hKey;
  26. }
  27. LONG REGKEY::Create(HKEY hKeyParent, LPCTSTR lpszKeyName,
  28. LPTSTR lpszClass, DWORD dwOptions, REGSAM samDesired,
  29. LPSECURITY_ATTRIBUTES lpSecAttr, LPDWORD lpdwDisposition)
  30. {
  31. assert(hKeyParent != NULL);
  32. DWORD dw;
  33. HKEY hKey = NULL;
  34. LONG lRes = RegCreateKeyEx(hKeyParent, lpszKeyName, 0,
  35. lpszClass, dwOptions, samDesired, lpSecAttr, &hKey, &dw);
  36. if (lpdwDisposition != NULL)
  37. *lpdwDisposition = dw;
  38. if (lRes == ERROR_SUCCESS)
  39. {
  40. lRes = Close();
  41. m_hKey = hKey;
  42. }
  43. return lRes;
  44. }
  45. LONG REGKEY::Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired)
  46. {
  47. assert(hKeyParent != NULL);
  48. HKEY hKey = NULL;
  49. LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyName, 0, samDesired, &hKey);
  50. if (lRes == ERROR_SUCCESS)
  51. {
  52. lRes = Close();
  53. assert(lRes == ERROR_SUCCESS);
  54. m_hKey = hKey;
  55. }
  56. return lRes;
  57. }
  58. LONG REGKEY::QueryValue(DWORD& dwValue, LPCTSTR lpszValueName)
  59. {
  60. DWORD dwType = NULL;
  61. DWORD dwCount = sizeof(DWORD);
  62. LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
  63. (LPBYTE)&dwValue, &dwCount);
  64. assert((lRes!=ERROR_SUCCESS) || (dwType == REG_DWORD));
  65. assert((lRes!=ERROR_SUCCESS) || (dwCount == sizeof(DWORD)));
  66. return lRes;
  67. }
  68. LONG REGKEY::QueryValue(LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount)
  69. {
  70. assert(pdwCount != NULL);
  71. DWORD dwType = NULL;
  72. LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
  73. (LPBYTE)szValue, pdwCount);
  74. assert((lRes!=ERROR_SUCCESS) || (dwType == REG_SZ) ||
  75. (dwType == REG_MULTI_SZ) || (dwType == REG_EXPAND_SZ));
  76. return lRes;
  77. }
  78. LONG WINAPI REGKEY::SetValue(HKEY hKeyParent, LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName)
  79. {
  80. assert(lpszValue != NULL);
  81. REGKEY key;
  82. LONG lRes = key.Create(hKeyParent, lpszKeyName);
  83. if (lRes == ERROR_SUCCESS)
  84. lRes = key.SetValue(lpszValue, lpszValueName);
  85. return lRes;
  86. }
  87. LONG REGKEY::SetKeyValue(LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName)
  88. {
  89. assert(lpszValue != NULL);
  90. REGKEY key;
  91. LONG lRes = key.Create(m_hKey, lpszKeyName);
  92. if (lRes == ERROR_SUCCESS)
  93. lRes = key.SetValue(lpszValue, lpszValueName);
  94. return lRes;
  95. }
  96. LONG REGKEY::SetValue(DWORD dwValue, LPCTSTR lpszValueName)
  97. {
  98. assert(m_hKey != NULL);
  99. return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_DWORD,
  100. (BYTE * const)&dwValue, sizeof(DWORD));
  101. }
  102. HRESULT REGKEY::SetValue(LPCTSTR lpszValue, LPCTSTR lpszValueName)
  103. {
  104. assert(lpszValue != NULL);
  105. assert(m_hKey != NULL);
  106. return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_SZ,
  107. (BYTE * const)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
  108. }
  109. //RecurseDeleteKey is necessary because on NT RegDeleteKey doesn't work if the
  110. //specified key has subkeys
  111. LONG REGKEY::RecurseDeleteKey(LPCTSTR lpszKey)
  112. {
  113. REGKEY key;
  114. LONG lRes = key.Open(m_hKey, lpszKey);
  115. if (lRes != ERROR_SUCCESS)
  116. return lRes;
  117. FILETIME time;
  118. TCHAR szBuffer[256];
  119. DWORD dwSize = 256;
  120. while (RegEnumKeyEx(key.m_hKey, 0, szBuffer, &dwSize, NULL, NULL, NULL,
  121. &time)==ERROR_SUCCESS)
  122. {
  123. lRes = key.RecurseDeleteKey(szBuffer);
  124. if (lRes != ERROR_SUCCESS)
  125. return lRes;
  126. dwSize = 256;
  127. }
  128. key.Close();
  129. return DeleteSubKey(lpszKey);
  130. }