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.

140 lines
3.8 KiB

  1. /***************************************************************************/
  2. /** Microsoft Windows **/
  3. /** Copyright(c) Microsoft Corp., 1991-1994 **/
  4. /***************************************************************************/
  5. /****************************************************************************
  6. regentry.h
  7. Mar. 94 JimH
  8. Wrapper for registry access
  9. Construct a RegEntry object by specifying the subkey (under
  10. HKEY_CURRENT_USER by default, but can be overridden.)
  11. All member functions are inline so there is minimal overhead.
  12. All member functions (except the destructor) set an internal
  13. error state which can be retrieved with GetError().
  14. Zero indicates no error.
  15. RegEntry works only with strings and DWORDS which are both set
  16. using the overloaded function SetValue()
  17. SetValue("valuename", "string");
  18. SetValue("valuename", 42);
  19. Values are retrieved with GetString() and GetNumber(). GetNumber()
  20. allows you to specificy a default if the valuename doesn't exist.
  21. DeleteValue() removes the valuename and value pair.
  22. ****************************************************************************/
  23. #ifndef REGENTRY_INC
  24. #define REGENTRY_INC
  25. #ifndef STRICT
  26. #define STRICT
  27. #endif
  28. #ifndef ERROR_SUCCESS
  29. #define ERROR_SUCCESS 0
  30. #endif
  31. #include <windows.h>
  32. class RegEntry
  33. {
  34. public:
  35. RegEntry(const TCHAR *pszSubKey, HKEY hkey = HKEY_CURRENT_USER);
  36. ~RegEntry() { if (_bConstructed) RegCloseKey(_hkey); }
  37. long GetError() { return _error; }
  38. long SetValue(const TCHAR *pszValue, const TCHAR *string);
  39. long SetValue(const TCHAR *pszValue, long dwNumber);
  40. TCHAR * GetString(const TCHAR *pszValue, TCHAR *string, DWORD length);
  41. long GetNumber(const TCHAR *pszValue, long dwDefault = 0);
  42. long DeleteValue(const TCHAR *pszValue);
  43. long FlushKey() { if (_bConstructed) return RegFlushKey(_hkey);
  44. else return NULL; }
  45. private:
  46. HKEY _hkey;
  47. long _error;
  48. long _bConstructed;
  49. };
  50. inline RegEntry::RegEntry(const TCHAR *pszSubKey, HKEY hkey)
  51. {
  52. _error = RegCreateKey(hkey, pszSubKey, &_hkey);
  53. _bConstructed = (_error == ERROR_SUCCESS);
  54. }
  55. inline long RegEntry::SetValue(const TCHAR *pszValue, const TCHAR *string)
  56. {
  57. if (_bConstructed)
  58. _error = RegSetValueEx(_hkey, pszValue, 0, REG_SZ,
  59. (BYTE *)string, sizeof(TCHAR) * (lstrlen(string)+1));
  60. return _error;
  61. }
  62. inline long RegEntry::SetValue(const TCHAR *pszValue, long dwNumber)
  63. {
  64. if (_bConstructed)
  65. _error = RegSetValueEx(_hkey, pszValue, 0, REG_BINARY,
  66. (BYTE *)&dwNumber, sizeof(dwNumber));
  67. return _error;
  68. }
  69. inline TCHAR *RegEntry::GetString(const TCHAR *pszValue, TCHAR *string, DWORD length)
  70. {
  71. DWORD dwType = REG_SZ;
  72. if (!_bConstructed)
  73. return NULL;
  74. _error = RegQueryValueEx(_hkey, pszValue, 0, &dwType, (LPBYTE)string,
  75. &length);
  76. if (_error)
  77. *string = '\0';
  78. return string;
  79. }
  80. inline long RegEntry::GetNumber(const TCHAR *pszValue, long dwDefault)
  81. {
  82. DWORD dwType = REG_BINARY;
  83. long dwNumber;
  84. DWORD dwSize = sizeof(dwNumber);
  85. if (!_bConstructed)
  86. return 0;
  87. _error = RegQueryValueEx(_hkey, pszValue, 0, &dwType, (LPBYTE)&dwNumber,
  88. &dwSize);
  89. if (_error)
  90. dwNumber = dwDefault;
  91. return dwNumber;
  92. }
  93. inline long RegEntry::DeleteValue(const TCHAR *pszValue)
  94. {
  95. if (_bConstructed)
  96. _error = RegDeleteValue(_hkey, pszValue);
  97. return _error;
  98. }
  99. #endif