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.

192 lines
4.1 KiB

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