Source code of Windows XP (NT5)
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.

161 lines
4.7 KiB

  1. /*--
  2. Copyright (c) 1995-2000 Microsoft Corporation. All rights reserved.
  3. Module Name: CREG.HXX
  4. Abstract: Registry helper class
  5. --*/
  6. #include "svsutil.hxx"
  7. /////////////////////////////////////////////////////////////////////////////
  8. // CReg: Registry helper class
  9. /////////////////////////////////////////////////////////////////////////////
  10. class CReg
  11. {
  12. private:
  13. HKEY m_hKey;
  14. int m_Index;
  15. LPBYTE m_lpbValue; // last value read, if any
  16. public:
  17. BOOL Create(HKEY hkRoot, LPCTSTR pszKey) {
  18. DWORD dwDisp;
  19. return ERROR_SUCCESS==RegCreateKeyEx(hkRoot, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisp);
  20. }
  21. BOOL Open(HKEY hkRoot, LPCTSTR pszKey, REGSAM sam=KEY_READ) {
  22. return ERROR_SUCCESS==RegOpenKeyEx(hkRoot, pszKey, 0, sam, &m_hKey);
  23. }
  24. CReg(HKEY hkRoot, LPCTSTR pszKey) {
  25. m_hKey = NULL;
  26. m_Index = 0;
  27. m_lpbValue = NULL;
  28. Open(hkRoot, pszKey);
  29. }
  30. CReg() {
  31. m_hKey = NULL;
  32. m_Index = 0;
  33. m_lpbValue = NULL;
  34. }
  35. ~CReg() {
  36. if(m_hKey) RegCloseKey(m_hKey);
  37. if (m_lpbValue)
  38. free(m_lpbValue);
  39. }
  40. void Reset() {
  41. if(m_hKey) RegCloseKey(m_hKey);
  42. if (m_lpbValue)
  43. free(m_lpbValue);
  44. m_hKey = NULL;
  45. m_Index = 0;
  46. m_lpbValue = NULL;
  47. }
  48. operator HKEY() { return m_hKey; }
  49. BOOL IsOK(void) { return m_hKey!=NULL; }
  50. DWORD NumSubkeys()
  51. {
  52. DWORD nSubKeys = 0;
  53. RegQueryInfoKey(m_hKey,0,0,0, &nSubKeys,0,0,0,0,0,0,0);
  54. return nSubKeys;
  55. }
  56. DWORD NumValues()
  57. {
  58. DWORD nValues = 0;
  59. RegQueryInfoKey(m_hKey,0,0,0,0,0,0,&nValues,0,0,0,0);
  60. return nValues;
  61. }
  62. BOOL EnumKey(LPTSTR psz, DWORD dwLen) {
  63. if(!m_hKey) return FALSE;
  64. // Note: EnumKey takes size in chars, not bytes!
  65. return ERROR_SUCCESS==RegEnumKeyEx(m_hKey, m_Index++, psz, &dwLen, NULL, NULL, NULL, NULL);
  66. }
  67. BOOL EnumValue(LPTSTR pszName, DWORD dwLenName, LPTSTR pszValue, DWORD dwLenValue) {
  68. DWORD dwType;
  69. if(!m_hKey) return FALSE;
  70. dwLenValue *= sizeof(TCHAR); // convert length in chars to bytes
  71. // Note: EnumValue takes size of Key in chars, but size of Value in bytes!!!
  72. return ERROR_SUCCESS==RegEnumValue(m_hKey, m_Index++, pszName, &dwLenName, NULL, &dwType, (LPBYTE)pszValue, &dwLenValue);
  73. }
  74. BOOL ValueSZ(LPCTSTR szName, LPTSTR szValue, DWORD dwLen) {
  75. if(!m_hKey) return FALSE;
  76. dwLen *= sizeof(TCHAR); // convert length in chars to bytes
  77. return ERROR_SUCCESS==RegQueryValueEx(m_hKey, szName, NULL, NULL, (LPBYTE)szValue, &dwLen);
  78. }
  79. DWORD ValueBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen) {
  80. if(!m_hKey) return 0;
  81. DWORD dwLenWant = dwLen;
  82. if(ERROR_SUCCESS==RegQueryValueEx(m_hKey, szName, NULL, NULL, lpbValue, &dwLen))
  83. return dwLen;
  84. else
  85. return 0;
  86. }
  87. LPCTSTR CReg::ValueSZ(LPCTSTR szName)
  88. {
  89. if(!m_hKey) return FALSE;
  90. DWORD dwLen = 0;
  91. if( (ERROR_SUCCESS != RegQueryValueEx(m_hKey, szName, NULL, NULL, NULL, &dwLen)) || (dwLen == 0) )
  92. return NULL;
  93. if (m_lpbValue)
  94. free(m_lpbValue);
  95. if( !(m_lpbValue = (BYTE *)malloc(dwLen)) ||
  96. (ERROR_SUCCESS != RegQueryValueEx(m_hKey, szName, NULL, NULL, m_lpbValue, &dwLen)) )
  97. return NULL;
  98. return (LPTSTR)m_lpbValue;
  99. }
  100. LPBYTE ValueBinary(LPCTSTR szName) {
  101. return (LPBYTE)ValueSZ(szName);
  102. }
  103. DWORD ValueDW(LPCTSTR szName, DWORD dwDefault=0) {
  104. DWORD dwValue = dwDefault;
  105. if(m_hKey)
  106. {
  107. DWORD dwLen = sizeof(DWORD);
  108. RegQueryValueEx(m_hKey, szName, NULL, NULL, (LPBYTE)&dwValue, &dwLen);
  109. }
  110. return dwValue;
  111. }
  112. BOOL SetSZ(LPCTSTR szName, LPCTSTR szValue, DWORD dwLen) {
  113. return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_SZ, (LPBYTE)szValue, sizeof(TCHAR)*dwLen);
  114. }
  115. BOOL SetSZ(LPCTSTR szName, LPCTSTR szValue) {
  116. return SetSZ(szName, szValue, 1+lstrlen(szValue));
  117. }
  118. BOOL SetDW(LPCTSTR szName, DWORD dwValue) {
  119. return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD));
  120. }
  121. BOOL SetBinary(LPCTSTR szName, LPBYTE lpbValue, DWORD dwLen) {
  122. return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_BINARY, lpbValue, dwLen);
  123. }
  124. BOOL SetMultiSZ(LPCTSTR szName, LPCTSTR lpszValue, DWORD dwLen) {
  125. return ERROR_SUCCESS==RegSetValueEx(m_hKey, szName, 0, REG_MULTI_SZ, (LPBYTE)lpszValue, sizeof(TCHAR)*dwLen);
  126. }
  127. BOOL DeleteValue(LPCTSTR szName) {
  128. return ERROR_SUCCESS==RegDeleteValue(m_hKey, szName);
  129. }
  130. };