Registry.cpp
上传用户:yffx2008
上传日期:2014-10-12
资源大小:12414k
文件大小:4k
源码类别:

交通/航空行业

开发平台:

Visual C++

  1. // Registry.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Registry.h"
  5. /////////////////////////////////////////////////////////////////////////////
  6. // CRegistry
  7. CRegistry::CRegistry(HKEY hKey)
  8. {
  9. m_hKey=hKey;
  10. }
  11. CRegistry::~CRegistry()
  12. {
  13. Close();
  14. }
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CRegistry Functions
  17. BOOL CRegistry::CreateKey(LPCTSTR lpSubKey)
  18. {
  19. ASSERT(m_hKey);
  20. ASSERT(lpSubKey);
  21. HKEY hKey;
  22. DWORD dw;
  23. long lReturn=RegCreateKeyEx(m_hKey,lpSubKey,0L,NULL,REG_OPTION_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dw);
  24.     
  25. if(lReturn==ERROR_SUCCESS)
  26. {
  27. m_hKey=hKey;
  28. return TRUE;
  29. }
  30. return FALSE;
  31. }
  32. BOOL CRegistry::Open(LPCTSTR lpSubKey)
  33. {
  34. ASSERT(m_hKey);
  35. ASSERT(lpSubKey);
  36.     
  37. HKEY hKey;
  38. long lReturn=RegOpenKeyEx(m_hKey,lpSubKey,0L,KEY_ALL_ACCESS,&hKey);
  39.     
  40. if(lReturn==ERROR_SUCCESS)
  41. {
  42.         m_hKey=hKey;
  43. return TRUE;
  44. }
  45. return FALSE;
  46. }
  47. void CRegistry::Close()
  48. {
  49. if(m_hKey)
  50. {
  51. RegCloseKey(m_hKey);
  52. m_hKey=NULL;
  53. }
  54. }
  55. BOOL CRegistry::DeleteValue(LPCTSTR lpValueName)
  56. {
  57. ASSERT(m_hKey);
  58. ASSERT(lpValueName);
  59. long lReturn=RegDeleteValue(m_hKey,lpValueName);
  60. if(lReturn==ERROR_SUCCESS)
  61. return TRUE;
  62. return FALSE;
  63. }
  64. BOOL CRegistry::DeleteKey(HKEY hKey, LPCTSTR lpSubKey)
  65. {
  66. ASSERT(hKey);
  67. ASSERT(lpSubKey);
  68. long lReturn=RegDeleteValue(hKey,lpSubKey);
  69. if(lReturn==ERROR_SUCCESS)
  70. return TRUE;
  71. return FALSE;
  72. }
  73. BOOL CRegistry::Write(LPCTSTR lpSubKey, int nVal)
  74. {
  75. ASSERT(m_hKey);
  76. ASSERT(lpSubKey);
  77. DWORD dwValue;
  78. dwValue=(DWORD)nVal;
  79. long lReturn=RegSetValueEx(m_hKey,lpSubKey,0L,REG_DWORD,(const BYTE *) &dwValue,sizeof(DWORD));
  80.     if(lReturn==ERROR_SUCCESS)
  81. return TRUE;
  82. return FALSE;
  83. }
  84. BOOL CRegistry::Write(LPCTSTR lpSubKey, DWORD dwVal)
  85. {
  86. ASSERT(m_hKey);
  87. ASSERT(lpSubKey);
  88. long lReturn=RegSetValueEx(m_hKey,lpSubKey,0L,REG_DWORD,(const BYTE *) &dwVal,sizeof(DWORD));
  89.     if(lReturn==ERROR_SUCCESS)
  90. return TRUE;
  91. return FALSE;
  92. }
  93. BOOL CRegistry::Write(LPCTSTR lpValueName, LPCTSTR lpValue)
  94. {
  95. ASSERT(m_hKey);
  96. ASSERT(lpValueName);
  97. ASSERT(lpValue);
  98. long lReturn=RegSetValueEx(m_hKey,lpValueName,0L,REG_SZ,(const BYTE *) lpValue,strlen(lpValue)+1);
  99.     if(lReturn==ERROR_SUCCESS)
  100. return TRUE;
  101. return FALSE;
  102. }
  103. BOOL CRegistry::Read(LPCTSTR lpValueName, int* pnVal)
  104. {
  105. ASSERT(m_hKey);
  106. ASSERT(lpValueName);
  107. ASSERT(pnVal);
  108. DWORD dwType;
  109. DWORD dwSize=sizeof(DWORD);
  110. DWORD dwDest;
  111. long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)&dwDest,&dwSize);
  112. if(lReturn==ERROR_SUCCESS)
  113. {
  114. *pnVal=(int)dwDest;
  115. return TRUE;
  116. }
  117. return FALSE;
  118. }
  119. BOOL CRegistry::Read(LPCTSTR lpValueName, DWORD* pdwVal)
  120. {
  121. ASSERT(m_hKey);
  122. ASSERT(lpValueName);
  123. ASSERT(pdwVal);
  124. DWORD dwType;
  125. DWORD dwSize=sizeof(DWORD);
  126. DWORD dwDest;
  127. long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)&dwDest,&dwSize);
  128. if(lReturn==ERROR_SUCCESS)
  129. {
  130. *pdwVal=dwDest;
  131. return TRUE;
  132. }
  133. return FALSE;
  134. }
  135. BOOL CRegistry::RestoreKey(LPCTSTR lpFileName)
  136. {
  137. ASSERT(m_hKey);
  138. ASSERT(lpFileName);
  139. long lReturn=RegRestoreKey(m_hKey,lpFileName,REG_WHOLE_HIVE_VOLATILE);
  140. if(lReturn==ERROR_SUCCESS)
  141. return TRUE;
  142. return FALSE;
  143. }
  144. BOOL CRegistry::SaveKey(LPCTSTR lpFileName)
  145. {
  146. ASSERT(m_hKey);
  147. ASSERT(lpFileName);
  148. long lReturn=RegSaveKey(m_hKey,lpFileName,NULL);
  149. if(lReturn==ERROR_SUCCESS)
  150. return TRUE;
  151. return FALSE;
  152. }
  153. BOOL CRegistry::Read(LPCTSTR lpValueName, CString* lpVal)
  154. {
  155. ASSERT(m_hKey);
  156. ASSERT(lpValueName);
  157. ASSERT(lpVal);
  158. DWORD dwType;
  159. DWORD dwSize=200;
  160. char szString[2550];
  161. long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)szString,&dwSize);
  162. if(lReturn==ERROR_SUCCESS)
  163. {
  164. *lpVal=szString;
  165. return TRUE;
  166. }
  167. return FALSE;
  168. }