PageRenderTime 39ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/win32/shellext/Registry.cpp

https://bitbucket.org/tortoisehg/hgtk/
C++ | 205 lines | 173 code | 17 blank | 15 comment | 13 complexity | 2b83ba812774f7423f3049aaa70cbea0 MD5 | raw file
Possible License(s): GPL-2.0
  1. // TortoiseSVN - a Windows shell extension for easy version control
  2. // Copyright (C) 2003-2006,2008-2009 - TortoiseSVN
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software Foundation,
  13. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. //
  15. #include "stdafx.h"
  16. #include "registry.h"
  17. //////////////////////////////////////////////////////////////////////////////////////////////
  18. #ifdef __CSTRINGT_H__
  19. CRegBase::CRegBase()
  20. {
  21. }
  22. CRegBase::CRegBase (const CString& key, bool force, HKEY base, REGSAM sam)
  23. : CRegBaseCommon<CString> (key, force, base, sam)
  24. {
  25. m_key.TrimLeft(_T("\\"));
  26. int backslashpos = m_key.ReverseFind('\\');
  27. m_path = m_key.Left(backslashpos);
  28. m_path.TrimRight(_T("\\"));
  29. m_key = m_key.Mid(backslashpos);
  30. m_key.Trim(_T("\\"));
  31. }
  32. #endif
  33. //////////////////////////////////////////////////////////////////////////////////////////////
  34. CRegStdBase::CRegStdBase()
  35. {
  36. }
  37. CRegStdBase::CRegStdBase (const tstring& key, bool force, HKEY base, REGSAM sam)
  38. : CRegBaseCommon<tstring> (key, force, base, sam)
  39. {
  40. tstring::size_type pos = key.find_last_of(_T('\\'));
  41. if (pos != tstring::npos)
  42. {
  43. m_path = key.substr(0, pos);
  44. m_key = key.substr(pos + 1);
  45. }
  46. }
  47. //////////////////////////////////////////////////////////////////////////////////////////////
  48. #ifdef __ATLTYPES_H__ // defines CRect
  49. CRegRect::CRegRect(void)
  50. : CRegTypedBase<CRect, CRegBase>(CRect(0,0,0,0))
  51. {
  52. }
  53. CRegRect::CRegRect(const CString& key, const CRect& def, bool force, HKEY base, REGSAM sam)
  54. : CRegTypedBase<CRect, CRegBase> (key, def, force, base, sam)
  55. {
  56. read();
  57. }
  58. void CRegRect::InternalRead (HKEY hKey, CRect& value)
  59. {
  60. DWORD size = 0;
  61. DWORD type = 0;
  62. RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
  63. auto_buffer<char> buffer (size);
  64. if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
  65. {
  66. ASSERT(type==REG_BINARY);
  67. value = CRect((LPRECT)buffer.get());
  68. }
  69. }
  70. void CRegRect::InternalWrite (HKEY hKey, const CRect& value)
  71. {
  72. LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)(LPCRECT)value, sizeof(value));
  73. }
  74. #endif
  75. //////////////////////////////////////////////////////////////////////////////////////////////
  76. #ifdef __ATLTYPES_H__ // defines CPoint
  77. CRegPoint::CRegPoint(void)
  78. : CRegTypedBase<CPoint, CRegBase>(CPoint(0,0))
  79. {
  80. }
  81. CRegPoint::CRegPoint(const CString& key, const CPoint& def, bool force, HKEY base, REGSAM sam)
  82. : CRegTypedBase<CPoint, CRegBase> (key, def, force, base, sam)
  83. {
  84. read();
  85. }
  86. void CRegPoint::InternalRead (HKEY hKey, CPoint& value)
  87. {
  88. DWORD size = 0;
  89. DWORD type = 0;
  90. RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
  91. auto_buffer<char> buffer(size);
  92. if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
  93. {
  94. ASSERT(type==REG_BINARY);
  95. value = CPoint(*(POINT*)buffer.get());
  96. }
  97. }
  98. void CRegPoint::InternalWrite (HKEY hKey, const CPoint& value)
  99. {
  100. LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)&value, sizeof(value));
  101. }
  102. #endif
  103. /////////////////////////////////////////////////////////////////////
  104. #ifdef __AFXCOLL_H__ // defines CStringList
  105. CRegistryKey::CRegistryKey(const CString& key, HKEY base, REGSAM sam)
  106. {
  107. m_base = base;
  108. m_hKey = NULL;
  109. m_sam = sam;
  110. m_path = key;
  111. m_path.TrimLeft(_T("\\"));
  112. }
  113. CRegistryKey::~CRegistryKey()
  114. {
  115. if (m_hKey)
  116. RegCloseKey(m_hKey);
  117. }
  118. DWORD CRegistryKey::createKey()
  119. {
  120. DWORD disp;
  121. DWORD rc = RegCreateKeyEx(m_base, m_path, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_WRITE|m_sam, NULL, &m_hKey, &disp);
  122. if (rc != ERROR_SUCCESS)
  123. {
  124. return rc;
  125. }
  126. return RegCloseKey(m_hKey);
  127. }
  128. DWORD CRegistryKey::removeKey()
  129. {
  130. RegOpenKeyEx(m_base, m_path, 0, KEY_WRITE|m_sam, &m_hKey);
  131. return SHDeleteKey(m_base, (LPCTSTR)m_path);
  132. }
  133. bool CRegistryKey::getValues(CStringList& values)
  134. {
  135. values.RemoveAll();
  136. if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
  137. {
  138. for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
  139. {
  140. TCHAR value[255];
  141. DWORD size = sizeof value / sizeof TCHAR;
  142. rc = RegEnumValue(m_hKey, i, value, &size, NULL, NULL, NULL, NULL);
  143. if (rc == ERROR_SUCCESS)
  144. {
  145. values.AddTail(value);
  146. }
  147. }
  148. }
  149. return values.GetCount() > 0;
  150. }
  151. bool CRegistryKey::getSubKeys(CStringList& subkeys)
  152. {
  153. subkeys.RemoveAll();
  154. if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
  155. {
  156. for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
  157. {
  158. TCHAR value[1024];
  159. DWORD size = sizeof value / sizeof TCHAR;
  160. FILETIME last_write_time;
  161. rc = RegEnumKeyEx(m_hKey, i, value, &size, NULL, NULL, NULL, &last_write_time);
  162. if (rc == ERROR_SUCCESS)
  163. {
  164. subkeys.AddTail(value);
  165. }
  166. }
  167. }
  168. return subkeys.GetCount() > 0;
  169. }
  170. #endif