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.

170 lines
6.5 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/registry/functions.hpp
  3. *
  4. * Purpose: Registry functions.
  5. *
  6. * Created: 20th November 1995
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1995-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file winstl/registry/functions.hpp
  40. *
  41. * \brief [C++ only] Simple and discrete registry functions, used by
  42. * the \ref group__library__windows_registry "Windows Registry" Library.
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_REGISTRY_HPP_FUNCTIONS
  45. #define WINSTL_INCL_WINSTL_REGISTRY_HPP_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_REGISTRY_HPP_FUNCTIONS_MAJOR 3
  48. # define WINSTL_VER_WINSTL_REGISTRY_HPP_FUNCTIONS_MINOR 1
  49. # define WINSTL_VER_WINSTL_REGISTRY_HPP_FUNCTIONS_REVISION 4
  50. # define WINSTL_VER_WINSTL_REGISTRY_HPP_FUNCTIONS_EDIT 49
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  56. # include <winstl/winstl.h>
  57. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  58. #ifndef WINSTL_INCL_WINSTL_REGISTRY_UTIL_HPP_DEFS
  59. # include <winstl/registry/util/defs.hpp>
  60. #endif /* !WINSTL_INCL_WINSTL_REGISTRY_UTIL_HPP_DEFS */
  61. #ifndef WINSTL_INCL_WINSTL_REGISTRY_HPP_REG_TRAITS
  62. # include <winstl/registry/reg_traits.hpp>
  63. #endif /* !WINSTL_INCL_WINSTL_REGISTRY_HPP_REG_TRAITS */
  64. /* /////////////////////////////////////////////////////////////////////////
  65. * Namespace
  66. */
  67. #ifndef _WINSTL_NO_NAMESPACE
  68. # if defined(_STLSOFT_NO_NAMESPACE) || \
  69. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  70. /* There is no stlsoft namespace, so must define ::winstl */
  71. namespace winstl
  72. {
  73. # else
  74. /* Define stlsoft::winstl_project */
  75. namespace stlsoft
  76. {
  77. namespace winstl_project
  78. {
  79. # endif /* _STLSOFT_NO_NAMESPACE */
  80. #endif /* !_WINSTL_NO_NAMESPACE */
  81. /* /////////////////////////////////////////////////////////////////////////
  82. * Functions
  83. */
  84. /** \brief Gets the contents of a registry key's string value into a
  85. * caller-supplied buffer.
  86. * \ingroup group__library__windows_registry
  87. *
  88. * \param hkey <code class="inout">[in]</code> Handle of the key whose values will be retrieved
  89. * \param name <code class="inout">[in]</code> The name of the value. May be <code>NULL</code> or the empty
  90. * string to access the key's default value
  91. * \param buffer <code class="inout">[in]</code> Pointer to the caller-allocated buffer into which the
  92. * value's string result will be written.
  93. * \param cchBuffer <code class="inout">[inout]</code> Specifies the size of the <code>buffer</code>
  94. * parameter and receives the number of bytes required for the whole value (including the
  95. * string's nul-terminating character).
  96. *
  97. * \return A Registry API status code indicating success or failure
  98. * \retval "ERROR_SUCCESS (==0)" The function completed successfully
  99. * \retval "any other value" The function failed, and the error code indicates why
  100. */
  101. template <ss_typename_param_k C>
  102. inline LONG reg_get_string_value(HKEY hkey, C const* name, C *buffer, ws_size_t &cchBuffer)
  103. {
  104. DWORD type;
  105. ws_size_t cbData = sizeof(C) * cchBuffer;
  106. LONG res = reg_traits<C>::reg_query_value(hkey, name, type, buffer, cbData);
  107. if(ERROR_SUCCESS == res)
  108. {
  109. cchBuffer = cbData / sizeof(C);
  110. }
  111. return res;
  112. }
  113. /** \brief Gets the contents of a registry key's DWORD value into a
  114. * caller-supplied variable.
  115. * \ingroup group__library__windows_registry
  116. *
  117. * \param hkey <code class="inout">[in]</code> Handle of the key whose values will be retrieved
  118. * \param name <code class="inout">[in]</code> The name of the value. May be <code>NULL</code> or the empty
  119. * string to access the key's default value
  120. * \param value <code class="inout">[out]</code> The value's value.
  121. *
  122. * \return A Registry API status code indicating success or failure
  123. * \retval "ERROR_SUCCESS (==0)" The function completed successfully
  124. * \retval "any other value" The function failed, and the error code indicates why
  125. *
  126. * \remarks If the function fails, the value of <code>value</code> is unchanged.
  127. */
  128. template <ss_typename_param_k C>
  129. inline LONG reg_get_dword_value(HKEY hkey, C const* name, DWORD& value)
  130. {
  131. DWORD type;
  132. ws_size_t cbData = sizeof(value);
  133. LONG res = reg_traits<C>::reg_query_value(hkey, name, type, &value, cbData);
  134. return res;
  135. }
  136. /* ////////////////////////////////////////////////////////////////////// */
  137. #ifndef _WINSTL_NO_NAMESPACE
  138. # if defined(_STLSOFT_NO_NAMESPACE) || \
  139. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  140. } // namespace winstl
  141. # else
  142. } // namespace winstl_project
  143. } // namespace stlsoft
  144. # endif /* _STLSOFT_NO_NAMESPACE */
  145. #endif /* !_WINSTL_NO_NAMESPACE */
  146. /* ////////////////////////////////////////////////////////////////////// */
  147. #endif /* WINSTL_INCL_WINSTL_REGISTRY_HPP_FUNCTIONS */
  148. /* ///////////////////////////// end of file //////////////////////////// */