environment.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2016 Klemens D. Morgenstern
  3. *
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * See http://www.boost.org/LICENSE_1_0.txt
  6. */
  7. #ifndef BOOST_WINAPI_ENVIRONMENT_HPP_INCLUDED_
  8. #define BOOST_WINAPI_ENVIRONMENT_HPP_INCLUDED_
  9. #include <boost/winapi/basic_types.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. #pragma once
  12. #endif
  13. #if BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  14. #include <boost/winapi/detail/header.hpp>
  15. #if defined(GetEnvironmentStrings)
  16. // Unlike most of the WinAPI, GetEnvironmentStrings is a real function and GetEnvironmentStringsA is a macro.
  17. // In UNICODE builds, GetEnvironmentStrings is also defined as a macro that redirects to GetEnvironmentStringsW,
  18. // and the narrow character version become inaccessible. Facepalm.
  19. #if defined(_MSC_VER) || defined(__GNUC__)
  20. #pragma push_macro("GetEnvironmentStrings")
  21. #endif
  22. #undef GetEnvironmentStrings
  23. #define BOOST_WINAPI_DETAIL_GET_ENVIRONMENT_STRINGS_UNDEFINED
  24. #endif // defined(GetEnvironmentStrings)
  25. #if !defined( BOOST_USE_WINDOWS_H )
  26. extern "C" {
  27. #if !defined( BOOST_NO_ANSI_APIS )
  28. BOOST_WINAPI_IMPORT boost::winapi::LPSTR_ BOOST_WINAPI_WINAPI_CC GetEnvironmentStrings();
  29. BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC FreeEnvironmentStringsA(boost::winapi::LPSTR_);
  30. BOOST_WINAPI_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC GetEnvironmentVariableA(
  31. boost::winapi::LPCSTR_ lpName,
  32. boost::winapi::LPSTR_ lpBuffer,
  33. boost::winapi::DWORD_ nSize
  34. );
  35. BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC SetEnvironmentVariableA(
  36. boost::winapi::LPCSTR_ lpName,
  37. boost::winapi::LPCSTR_ lpValue
  38. );
  39. #endif // !defined( BOOST_NO_ANSI_APIS )
  40. BOOST_WINAPI_IMPORT boost::winapi::LPWSTR_ BOOST_WINAPI_WINAPI_CC GetEnvironmentStringsW();
  41. BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC FreeEnvironmentStringsW(boost::winapi::LPWSTR_);
  42. BOOST_WINAPI_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC GetEnvironmentVariableW(
  43. boost::winapi::LPCWSTR_ lpName,
  44. boost::winapi::LPWSTR_ lpBuffer,
  45. boost::winapi::DWORD_ nSize
  46. );
  47. BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC SetEnvironmentVariableW(
  48. boost::winapi::LPCWSTR_ lpName,
  49. boost::winapi::LPCWSTR_ lpValue
  50. );
  51. } // extern "C"
  52. #endif // !defined( BOOST_USE_WINDOWS_H )
  53. namespace boost {
  54. namespace winapi {
  55. #if !defined( BOOST_NO_ANSI_APIS )
  56. using ::GetEnvironmentStrings;
  57. using ::FreeEnvironmentStringsA;
  58. using ::GetEnvironmentVariableA;
  59. using ::SetEnvironmentVariableA;
  60. #endif // !defined( BOOST_NO_ANSI_APIS )
  61. using ::GetEnvironmentStringsW;
  62. using ::FreeEnvironmentStringsW;
  63. using ::GetEnvironmentVariableW;
  64. using ::SetEnvironmentVariableW;
  65. template< typename Char >
  66. Char* get_environment_strings();
  67. #if !defined( BOOST_NO_ANSI_APIS )
  68. template< >
  69. BOOST_FORCEINLINE char* get_environment_strings< char >()
  70. {
  71. return GetEnvironmentStrings();
  72. }
  73. BOOST_FORCEINLINE BOOL_ free_environment_strings(LPSTR_ p)
  74. {
  75. return FreeEnvironmentStringsA(p);
  76. }
  77. BOOST_FORCEINLINE DWORD_ get_environment_variable(LPCSTR_ name, LPSTR_ buffer, DWORD_ size)
  78. {
  79. return GetEnvironmentVariableA(name, buffer, size);
  80. }
  81. BOOST_FORCEINLINE BOOL_ set_environment_variable(LPCSTR_ name, LPCSTR_ value)
  82. {
  83. return SetEnvironmentVariableA(name, value);
  84. }
  85. #endif // !defined( BOOST_NO_ANSI_APIS )
  86. template< >
  87. BOOST_FORCEINLINE wchar_t* get_environment_strings< wchar_t >()
  88. {
  89. return GetEnvironmentStringsW();
  90. }
  91. BOOST_FORCEINLINE BOOL_ free_environment_strings(LPWSTR_ p)
  92. {
  93. return FreeEnvironmentStringsW(p);
  94. }
  95. BOOST_FORCEINLINE DWORD_ get_environment_variable(LPCWSTR_ name, LPWSTR_ buffer, DWORD_ size)
  96. {
  97. return GetEnvironmentVariableW(name, buffer, size);
  98. }
  99. BOOST_FORCEINLINE BOOL_ set_environment_variable(LPCWSTR_ name, LPCWSTR_ value)
  100. {
  101. return SetEnvironmentVariableW(name, value);
  102. }
  103. } // namespace winapi
  104. } // namespace boost
  105. #if defined(BOOST_WINAPI_DETAIL_GET_ENVIRONMENT_STRINGS_UNDEFINED)
  106. #if defined(_MSC_VER) || defined(__GNUC__)
  107. #pragma pop_macro("GetEnvironmentStrings")
  108. #elif defined(UNICODE)
  109. #define GetEnvironmentStrings GetEnvironmentStringsW
  110. #endif
  111. #undef BOOST_WINAPI_DETAIL_GET_ENVIRONMENT_STRINGS_UNDEFINED
  112. #endif // defined(BOOST_WINAPI_DETAIL_GET_ENVIRONMENT_STRINGS_UNDEFINED)
  113. #include <boost/winapi/detail/footer.hpp>
  114. #endif // BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  115. #endif // BOOST_WINAPI_ENVIRONMENT_HPP_INCLUDED_