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.

198 lines
4.0 KiB

  1. /*++
  2. Copyright (C) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. reg.cpp
  5. Abstract:
  6. Implementation of the registry helper class
  7. History:
  8. --*/
  9. #include "precomp.h"
  10. #include <stdio.h>
  11. #define DEPRECATE_SUPPORTED
  12. #define STRSAFE_NO_CB_FUNCTIONS
  13. #include <strsafe.h>
  14. #include "reg.h"
  15. Registry::Registry(char *pszLocalMachineStartKey)
  16. {
  17. hPrimaryKey = 0;
  18. hSubkey = 0;
  19. m_nStatus = Open(HKEY_LOCAL_MACHINE, pszLocalMachineStartKey);
  20. hSubkey = hPrimaryKey;
  21. }
  22. Registry::~Registry()
  23. {
  24. if (hSubkey)
  25. RegCloseKey(hSubkey);
  26. if (hPrimaryKey != hSubkey)
  27. RegCloseKey(hPrimaryKey);
  28. }
  29. int Registry::Open(HKEY hStart, const char *pszStartKey)
  30. {
  31. int nStatus = no_error;
  32. DWORD dwDisp = 0;
  33. m_nLastError = RegCreateKeyEx(hStart, pszStartKey,
  34. 0, 0, 0,
  35. KEY_ALL_ACCESS, 0, &hPrimaryKey, &dwDisp);
  36. if (m_nLastError != 0)
  37. nStatus = failed;
  38. return nStatus;
  39. }
  40. char* Registry::GetMultiStr(const char *pszValueName, DWORD &dwSize)
  41. {
  42. //Find out the size of the buffer required
  43. DWORD dwType;
  44. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, &dwType, NULL, &dwSize);
  45. //If the error is an unexpected one bail out
  46. if ((m_nLastError != ERROR_SUCCESS) || (dwType != REG_MULTI_SZ))
  47. {
  48. dwSize = 0;
  49. return NULL;
  50. }
  51. if (dwSize == 0)
  52. {
  53. return NULL;
  54. }
  55. //allocate the buffer required
  56. char *pData = new char[dwSize];
  57. if (!pData)
  58. {
  59. dwSize = 0;
  60. return NULL;
  61. }
  62. //get the values
  63. m_nLastError = RegQueryValueEx(hSubkey,
  64. pszValueName,
  65. 0,
  66. &dwType,
  67. LPBYTE(pData),
  68. &dwSize);
  69. //if an error bail out
  70. if (m_nLastError != 0)
  71. {
  72. delete [] pData;
  73. dwSize = 0;
  74. return NULL;
  75. }
  76. return pData;
  77. }
  78. int Registry::SetMultiStr(const char *pszValueName, const char*pszValue, DWORD dwSize)
  79. {
  80. m_nLastError = RegSetValueEx(hSubkey,
  81. pszValueName,
  82. 0,
  83. REG_MULTI_SZ,
  84. LPBYTE(pszValue),
  85. dwSize);
  86. if (m_nLastError != 0)
  87. return failed;
  88. return no_error;
  89. }
  90. int Registry::GetStr(const char *pszValueName, char **pValue)
  91. {
  92. *pValue = 0;
  93. DWORD dwSize = 0;
  94. DWORD dwType = 0;
  95. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, &dwType,
  96. 0, &dwSize);
  97. if (m_nLastError != 0)
  98. return failed;
  99. if (dwType != REG_SZ && dwType != REG_EXPAND_SZ)
  100. return failed;
  101. char *p = new char[dwSize];
  102. if (!p)
  103. return failed;
  104. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, &dwType,
  105. LPBYTE(p), &dwSize);
  106. if (m_nLastError != 0)
  107. {
  108. delete [] p;
  109. return failed;
  110. }
  111. if(dwType == REG_EXPAND_SZ)
  112. {
  113. char tTemp;
  114. // Get the initial length
  115. DWORD nSize = ExpandEnvironmentStrings(p,&tTemp,1) + 1;
  116. TCHAR* pTemp = new TCHAR[nSize+1];
  117. if (!pTemp)
  118. return failed;
  119. if (!ExpandEnvironmentStrings(p,pTemp,nSize+1))
  120. {
  121. delete [] p;
  122. delete [] pTemp;
  123. return failed;
  124. }
  125. delete [] p;
  126. *pValue = pTemp;
  127. }
  128. else
  129. *pValue = p;
  130. return no_error;
  131. }
  132. int Registry::DeleteEntry(const char *pszValueName)
  133. {
  134. m_nLastError = RegDeleteValue( hSubkey, pszValueName);
  135. if (m_nLastError != 0)
  136. {
  137. return failed;
  138. }
  139. else
  140. return no_error;
  141. }
  142. int Registry::SetStr(char *pszValueName, char *pszValue)
  143. {
  144. m_nLastError = RegSetValueEx(hSubkey, pszValueName, 0, REG_SZ, LPBYTE(pszValue),
  145. strlen(pszValue) + 1);
  146. if (m_nLastError != 0)
  147. return failed;
  148. return no_error;
  149. }
  150. int Registry::GetDWORD(TCHAR *pszValueName, DWORD *pdwValue)
  151. {
  152. DWORD dwSize = sizeof(DWORD);
  153. DWORD dwType = 0;
  154. m_nLastError = RegQueryValueEx(hSubkey, pszValueName, 0, &dwType,
  155. LPBYTE(pdwValue), &dwSize);
  156. if (m_nLastError != 0)
  157. return failed;
  158. if (dwType != REG_DWORD)
  159. return failed;
  160. return no_error;
  161. }