Source code of Windows XP (NT5)
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.

163 lines
5.2 KiB

  1. //--------------------------------------------------------------------
  2. // w32timep - implementation
  3. // Copyright (C) Microsoft Corporation, 2000
  4. //
  5. // Created by: Duncan Bryce (duncanb), 12-07-00
  6. //
  7. // Contains methods to configure or query the windows time service
  8. //
  9. #include <windows.h>
  10. #include <w32timep.h>
  11. #include "DebugWPrintf.h"
  12. #include "ErrorHandling.h"
  13. #include "W32TmConsts.h"
  14. struct PropertyTable {
  15. DWORD dwProperty;
  16. LPWSTR pwszRegKeyName;
  17. LPWSTR pwszRegValueName;
  18. } g_rgProperties[] = {
  19. { W32TIME_CONFIG_SPECIAL_POLL_INTERVAL, wszNtpClientRegKeyConfig, wszNtpClientRegValueSpecialPollInterval },
  20. { W32TIME_CONFIG_MANUAL_PEER_LIST, wszW32TimeRegKeyParameters, wszW32TimeRegValueNtpServer }
  21. };
  22. //-------------------------------------------------------------------------------------
  23. HRESULT MODULEPRIVATE myRegOpenKeyForProperty(IN DWORD dwProperty, OUT HKEY *phKey) {
  24. DWORD dwRetval;
  25. HRESULT hr;
  26. LPWSTR pwszKeyName = NULL;
  27. for (DWORD dwIndex = 0; dwIndex < ARRAYSIZE(g_rgProperties); dwIndex++) {
  28. if (dwProperty == g_rgProperties[dwIndex].dwProperty) {
  29. pwszKeyName = g_rgProperties[dwIndex].pwszRegKeyName;
  30. break;
  31. }
  32. }
  33. if (NULL == pwszKeyName) {
  34. hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  35. _JumpError(hr, error, "myRegOpenKeyForProperty: key not found");
  36. }
  37. dwRetval = RegOpenKeyEx(HKEY_LOCAL_MACHINE, pwszKeyName, 0, KEY_QUERY_VALUE | KEY_SET_VALUE, phKey);
  38. if (ERROR_SUCCESS != dwRetval) {
  39. hr = HRESULT_FROM_WIN32(dwRetval);
  40. _JumpError(hr, error, "RegOpenKeyEx");
  41. }
  42. hr = S_OK;
  43. error:
  44. return hr;
  45. }
  46. //-------------------------------------------------------------------------------------
  47. HRESULT MODULEPRIVATE myRegQueryValueForProperty(DWORD dwProperty, HKEY hKey, DWORD *pdwType, BYTE *pbValue, DWORD *pdwSize) {
  48. DWORD dwRetval;
  49. HRESULT hr;
  50. LPWSTR pwszValueName = NULL;
  51. for (DWORD dwIndex = 0; dwIndex < ARRAYSIZE(g_rgProperties); dwIndex++) {
  52. if (dwProperty == g_rgProperties[dwIndex].dwProperty) {
  53. pwszValueName = g_rgProperties[dwIndex].pwszRegValueName;
  54. break;
  55. }
  56. }
  57. if (NULL == pwszValueName) {
  58. hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  59. _JumpError(hr, error, "myRegQueryValueForProperty: value not found");
  60. }
  61. dwRetval = RegQueryValueEx(hKey, pwszValueName, NULL, pdwType, pbValue, pdwSize);
  62. if (ERROR_SUCCESS != dwRetval) {
  63. hr = HRESULT_FROM_WIN32(dwRetval);
  64. _JumpError(hr, error, "RegQueryValueEx");
  65. }
  66. hr = S_OK;
  67. error:
  68. return hr;
  69. }
  70. //-------------------------------------------------------------------------------------
  71. HRESULT MODULEPRIVATE myRegSetValueForProperty(DWORD dwProperty, HKEY hKey, DWORD dwType, BYTE *pbValue, DWORD dwSize) {
  72. DWORD dwRetval;
  73. HRESULT hr;
  74. LPWSTR pwszValueName = NULL;
  75. for (DWORD dwIndex = 0; dwIndex < ARRAYSIZE(g_rgProperties); dwIndex++) {
  76. if (dwProperty == g_rgProperties[dwIndex].dwProperty) {
  77. pwszValueName = g_rgProperties[dwIndex].pwszRegValueName;
  78. break;
  79. }
  80. }
  81. if (NULL == pwszValueName) {
  82. hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  83. _JumpError(hr, error, "myRegSetValueForProperty: value not found");
  84. }
  85. dwRetval = RegSetValueEx(hKey, pwszValueName, NULL, dwType, pbValue, dwSize);
  86. if (ERROR_SUCCESS != dwRetval) {
  87. hr = HRESULT_FROM_WIN32(dwRetval);
  88. _JumpError(hr, error, "RegSetValueEx");
  89. }
  90. hr = S_OK;
  91. error:
  92. return hr;
  93. }
  94. //-------------------------------------------------------------------------------------
  95. // MODULEPUBLIC functions
  96. //
  97. //-------------------------------------------------------------------------------------
  98. HRESULT W32TimeQueryConfig(IN DWORD dwProperty,
  99. OUT DWORD *pdwType,
  100. IN OUT BYTE *pbConfig,
  101. IN OUT DWORD *pdwSize)
  102. {
  103. HKEY hKey = NULL;
  104. HRESULT hr;
  105. if (NULL == pdwType || NULL == pbConfig || NULL == pdwSize) {
  106. hr = E_INVALIDARG;
  107. _JumpError(hr, error, "W32TimeQueryConfig: input validation");
  108. }
  109. hr = myRegOpenKeyForProperty(dwProperty, &hKey);
  110. _JumpIfError(hr, error, "myOpenRegKeyForProperty");
  111. hr = myRegQueryValueForProperty(dwProperty, hKey, pdwType, pbConfig, pdwSize);
  112. _JumpIfError(hr, error, "myQueryRegValueForProperty");
  113. hr = S_OK;
  114. error:
  115. if (NULL != hKey) { RegCloseKey(hKey); }
  116. return hr;
  117. }
  118. //-------------------------------------------------------------------------------------
  119. HRESULT W32TimeSetConfig(IN DWORD dwProperty,
  120. IN DWORD dwType,
  121. IN BYTE *pbConfig,
  122. IN DWORD dwSize)
  123. {
  124. DWORD dwRetval;
  125. HKEY hKey = NULL;
  126. HRESULT hr;
  127. hr = myRegOpenKeyForProperty(dwProperty, &hKey);
  128. _JumpIfError(hr, error, "myRegOpenKeyForProperty");
  129. hr = myRegSetValueForProperty(dwProperty, hKey, dwType, pbConfig, dwSize);
  130. _JumpIfError(hr, error, "myRegSetValueForProperty");
  131. hr = S_OK;
  132. error:
  133. if (NULL != hKey) { RegCloseKey(hKey); }
  134. return hr;
  135. }