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.

248 lines
6.9 KiB

  1. /************************************************************************
  2. Copyright (c) 2000 - 2000 Microsoft Corporation
  3. Module Name :
  4. cfreg.cpp
  5. Abstract :
  6. Registry wrapper functions.
  7. Author :
  8. Revision History :
  9. ***********************************************************************/
  10. #include "qmgrlibp.h"
  11. #if !defined(BITS_V12_ON_NT4)
  12. #include "cfreg.tmh"
  13. #endif
  14. ////////////////////////////////////////////////////////////////////////////
  15. //
  16. // Public Function GetRegStringValue()
  17. // Read the registry value of timestamp for last detection
  18. // Input: Name of value
  19. // Output: SYSTEMTIME structure contains the time
  20. // Return: HRESULT flag indicating the success of this function
  21. //
  22. ////////////////////////////////////////////////////////////////////////////
  23. HRESULT GetRegStringValue(LPCTSTR lpszValueName, LPTSTR lpszBuffer, int iBufferSize)
  24. {
  25. HKEY hKey;
  26. DWORD dwType = REG_SZ;
  27. DWORD dwSize = iBufferSize;
  28. DWORD dwRet;
  29. if (lpszValueName == NULL || lpszBuffer == NULL)
  30. {
  31. return E_INVALIDARG;
  32. }
  33. //
  34. // query the last timestamp value
  35. //
  36. dwRet = RegQueryValueEx(
  37. g_GlobalInfo->m_QmgrRegistryRoot,
  38. lpszValueName,
  39. NULL,
  40. &dwType,
  41. (LPBYTE)lpszBuffer,
  42. &dwSize);
  43. if (dwRet == ERROR_SUCCESS && dwType == REG_SZ)
  44. {
  45. return S_OK;
  46. }
  47. return E_FAIL;
  48. }
  49. ////////////////////////////////////////////////////////////////////////////
  50. //
  51. // Public Function SetRegStringValue()
  52. // Set the registry value of timestamp as current system local time
  53. // Input: name of the value to set. pointer to the time structure to set time. if null,
  54. // we use current system time.
  55. // Output: None
  56. // Return: HRESULT flag indicating the success of this function
  57. //
  58. ////////////////////////////////////////////////////////////////////////////
  59. HRESULT SetRegStringValue(LPCTSTR lpszValueName, LPCTSTR lpszNewValue)
  60. {
  61. HKEY hKey;
  62. HRESULT hRet = E_FAIL;
  63. DWORD dwResult;
  64. if (lpszValueName == NULL || lpszNewValue == NULL)
  65. {
  66. return E_INVALIDARG;
  67. }
  68. //
  69. // set the time to the lasttimestamp value
  70. //
  71. hRet = (RegSetValueEx( //SEC: REVIEWED 2002-03-28
  72. g_GlobalInfo->m_QmgrRegistryRoot,
  73. lpszValueName,
  74. 0,
  75. REG_SZ,
  76. (const unsigned char *)lpszNewValue,
  77. lstrlen(lpszNewValue) + 1 // SEC: REVIEWED 2002-03-28
  78. ) == ERROR_SUCCESS) ? S_OK : E_FAIL;
  79. return hRet;
  80. }
  81. ////////////////////////////////////////////////////////////////////////////
  82. //
  83. // Public Function DeleteRegStringValue()
  84. // Delete the registry value entry
  85. // Input: name of the value to entry,
  86. // Output: None
  87. // Return: HRESULT flag indicating the success of this function
  88. //
  89. ////////////////////////////////////////////////////////////////////////////
  90. HRESULT DeleteRegStringValue(LPCTSTR lpszValueName)
  91. {
  92. HKEY hKey;
  93. HRESULT hRet = E_FAIL;
  94. DWORD dwResult;
  95. if (lpszValueName == NULL)
  96. {
  97. return E_INVALIDARG;
  98. }
  99. //
  100. // set the time to the lasttimestamp value
  101. //
  102. hRet = (RegDeleteValue(
  103. g_GlobalInfo->m_QmgrRegistryRoot,
  104. lpszValueName
  105. ) == ERROR_SUCCESS) ? S_OK : E_FAIL;
  106. return hRet;
  107. }
  108. ////////////////////////////////////////////////////////////////////////////
  109. //
  110. // Public Function GetRegDWordValue()
  111. // Get a DWORD from specified regustry value name
  112. // Input: name of the value to retrieve value
  113. // Output: pointer to the retrieved value
  114. // Return: HRESULT flag indicating the success of this function
  115. //
  116. ////////////////////////////////////////////////////////////////////////////
  117. HRESULT GetRegDWordValue(LPCTSTR lpszValueName, LPDWORD pdwValue)
  118. {
  119. HKEY hKey;
  120. int iRet;
  121. DWORD dwType = REG_DWORD, dwSize = sizeof(DWORD);
  122. if (lpszValueName == NULL)
  123. {
  124. return E_INVALIDARG;
  125. }
  126. //
  127. // open critical fix key
  128. //
  129. iRet = RegOpenKeyEx(
  130. HKEY_LOCAL_MACHINE,
  131. C_QMGR_REG_KEY,
  132. 0,
  133. KEY_READ,
  134. &hKey);
  135. if (iRet == ERROR_SUCCESS)
  136. {
  137. //
  138. // query the last timestamp value
  139. //
  140. iRet = RegQueryValueEx( //SEC: REVIEWED 2002-03-28
  141. hKey,
  142. lpszValueName,
  143. NULL,
  144. &dwType,
  145. (LPBYTE)pdwValue,
  146. &dwSize);
  147. RegCloseKey(hKey);
  148. if (iRet == ERROR_SUCCESS)
  149. {
  150. if (dwType == REG_DWORD)
  151. {
  152. return S_OK;
  153. }
  154. return E_FAIL;
  155. }
  156. }
  157. return HRESULT_FROM_WIN32( iRet );
  158. }
  159. ////////////////////////////////////////////////////////////////////////////
  160. //
  161. // Public Function SetRegDWordValue()
  162. // Set the registry value as a DWORD
  163. // Input: name of the value to set. value to set
  164. // Output: None
  165. // Return: HRESULT flag indicating the success of this function
  166. //
  167. ////////////////////////////////////////////////////////////////////////////
  168. HRESULT SetRegDWordValue(LPCTSTR lpszValueName, DWORD dwValue)
  169. {
  170. HKEY hKey;
  171. HRESULT hRet = E_FAIL;
  172. DWORD dwResult;
  173. if (lpszValueName == NULL)
  174. {
  175. return E_INVALIDARG;
  176. }
  177. //
  178. // open the key
  179. //
  180. if (RegCreateKeyEx( //SEC: REVIEWED 2002-03-28
  181. HKEY_LOCAL_MACHINE, // root key
  182. C_QMGR_REG_KEY, // subkey
  183. 0, // reserved
  184. NULL, // class name
  185. REG_OPTION_NON_VOLATILE, // option
  186. KEY_WRITE, // security
  187. NULL, // security attribute
  188. &hKey,
  189. &dwResult) == ERROR_SUCCESS)
  190. {
  191. //
  192. // set the time to the lasttimestamp value
  193. //
  194. hRet = (RegSetValueEx( //SEC: REVIEWED 2002-03-28
  195. hKey,
  196. lpszValueName,
  197. 0,
  198. REG_DWORD,
  199. (LPBYTE)&dwValue,
  200. sizeof(DWORD)
  201. ) == ERROR_SUCCESS) ? S_OK : E_FAIL;
  202. RegCloseKey(hKey);
  203. }
  204. return hRet;
  205. }