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.

149 lines
4.2 KiB

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright(C) 1998-1999 Microsoft Corporation all rights reserved.
  4. //
  5. // Module: getvalue.cpp
  6. //
  7. // Project: Chameleon
  8. //
  9. // Description: Get/Set a property value from a specified object
  10. //
  11. // Log:
  12. //
  13. // When Who What
  14. // ---- --- ----
  15. // 05/06/1999 TLP Initial Version
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include "stdafx.h"
  19. #include <getvalue.h>
  20. #include <propertybagfactory.h>
  21. #include <satrace.h>
  22. //////////////////////////////////////////////////////////////////////////////
  23. //
  24. // Function: GetValue()
  25. //
  26. // Synopsis: Get a value from the specified object
  27. //
  28. //////////////////////////////////////////////////////////////////////////////
  29. bool
  30. GetObjectValue(
  31. /*[in]*/ LPCWSTR pszObjectPath,
  32. /*[in]*/ LPCWSTR pszValueName,
  33. /*[in]*/ VARIANT* pValue,
  34. /*[in]*/ UINT uExpectedType
  35. )
  36. {
  37. bool bReturn = false;
  38. try
  39. {
  40. do
  41. {
  42. CLocationInfo LocInfo(HKEY_LOCAL_MACHINE, pszObjectPath);
  43. PPROPERTYBAG pBag = ::MakePropertyBag(
  44. PROPERTY_BAG_REGISTRY,
  45. LocInfo
  46. );
  47. if ( ! pBag.IsValid() )
  48. {
  49. SATraceString("GetValue() - Could not locate registry key");
  50. break;
  51. }
  52. if ( ! pBag->open() )
  53. {
  54. SATraceString("GetValue() - Could not open registry key");
  55. break;
  56. }
  57. if ( ! pBag->get(pszValueName, pValue) )
  58. {
  59. SATracePrintf("GetValue() - Could not get value '%ls'", pszValueName);
  60. break;
  61. }
  62. else
  63. {
  64. if ( uExpectedType != V_VT(pValue) )
  65. {
  66. SATracePrintf("GetValue() - Unexpected value data type for '%ls', pszValueName");
  67. break;
  68. }
  69. else
  70. {
  71. bReturn = true;
  72. }
  73. }
  74. } while ( FALSE );
  75. }
  76. catch(...)
  77. {
  78. SATraceString("GetValue() - Caught unhandled exception");
  79. }
  80. return bReturn;
  81. }
  82. //////////////////////////////////////////////////////////////////////////////
  83. //
  84. // Function: SetObjectValue()
  85. //
  86. // Synopsis: Set a value in a specified object
  87. //
  88. //////////////////////////////////////////////////////////////////////////////
  89. bool
  90. SetObjectValue(
  91. /*[in]*/ LPCWSTR pszObjectPath,
  92. /*[in]*/ LPCWSTR pszValueName,
  93. /*[in]*/ VARIANT* pValue
  94. )
  95. {
  96. bool bReturn = false;
  97. try
  98. {
  99. do
  100. {
  101. CLocationInfo LocInfo(HKEY_LOCAL_MACHINE, pszObjectPath);
  102. PPROPERTYBAG pBag = ::MakePropertyBag(
  103. PROPERTY_BAG_REGISTRY,
  104. LocInfo
  105. );
  106. if ( ! pBag.IsValid() )
  107. {
  108. SATraceString("SetValue() - Could not locate registry key");
  109. break;
  110. }
  111. if ( ! pBag->open() )
  112. {
  113. SATraceString("SetValue() - Could not open registry key");
  114. break;
  115. }
  116. if ( ! pBag->put(pszValueName, pValue) )
  117. {
  118. SATracePrintf("SetValue() - Could not set value '%ls'", pszValueName);
  119. break;
  120. }
  121. if ( ! pBag->save ())
  122. {
  123. SATracePrintf("SetValue() - Could not save value '%ls'", pszValueName);
  124. break;
  125. }
  126. //
  127. // success
  128. //
  129. bReturn = true;
  130. } while ( FALSE );
  131. }
  132. catch(...)
  133. {
  134. SATraceString("SetValue() - Caught unhandled exception");
  135. }
  136. return bReturn;
  137. }