environment.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #if defined(GetEnvironmentStrings)
  15. // Unlike most of the WinAPI, GetEnvironmentStrings is a real function and GetEnvironmentStringsA is a macro.
  16. // In UNICODE builds, GetEnvironmentStrings is also defined as a macro that redirects to GetEnvironmentStringsW,
  17. // and the narrow character version become inaccessible. Facepalm.
  18. #if defined(_MSC_VER) || defined(__GNUC__)
  19. #pragma push_macro("GetEnvironmentStrings")
  20. #endif
  21. #undef GetEnvironmentStrings
  22. #define BOOST_WINAPI_DETAIL_GET_ENVIRONMENT_STRINGS_UNDEFINED
  23. #endif // defined(GetEnvironmentStrings)
  24. #if !defined( BOOST_USE_WINDOWS_H )
  25. extern "C" {
  26. #if !defined( BOOST_NO_ANSI_APIS )
  27. BOOST_SYMBOL_IMPORT boost::winapi::LPSTR_ BOOST_WINAPI_WINAPI_CC GetEnvironmentStrings();
  28. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC FreeEnvironmentStringsA(boost::winapi::LPSTR_);
  29. BOOST_SYMBOL_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC GetEnvironmentVariableA(
  30. boost::winapi::LPCSTR_ lpName,
  31. boost::winapi::LPSTR_ lpBuffer,
  32. boost::winapi::DWORD_ nSize
  33. );
  34. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC SetEnvironmentVariableA(
  35. boost::winapi::LPCSTR_ lpName,
  36. boost::winapi::LPCSTR_ lpValue
  37. );
  38. #endif // !defined( BOOST_NO_ANSI_APIS )
  39. BOOST_SYMBOL_IMPORT boost::winapi::LPWSTR_ BOOST_WINAPI_WINAPI_CC GetEnvironmentStringsW();
  40. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC FreeEnvironmentStringsW(boost::winapi::LPWSTR_);
  41. BOOST_SYMBOL_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC GetEnvironmentVariableW(
  42. boost::winapi::LPCWSTR_ lpName,
  43. boost::winapi::LPWSTR_ lpBuffer,
  44. boost::winapi::DWORD_ nSize
  45. );
  46. BOOST_SYMBOL_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC SetEnvironmentVariableW(
  47. boost::winapi::LPCWSTR_ lpName,
  48. boost::winapi::LPCWSTR_ lpValue
  49. );
  50. } // extern "C"
  51. #endif // !defined( BOOST_USE_WINDOWS_H )
  52. namespace boost {
  53. namespace winapi {
  54. #if !defined( BOOST_NO_ANSI_APIS )
  55. using ::GetEnvironmentStrings;
  56. using ::FreeEnvironmentStringsA;
  57. using ::GetEnvironmentVariableA;
  58. using ::SetEnvironmentVariableA;
  59. #endif // !defined( BOOST_NO_ANSI_APIS )
  60. using ::GetEnvironmentStringsW;
  61. using ::FreeEnvironmentStringsW;
  62. using ::GetEnvironmentVariableW;
  63. using ::SetEnvironmentVariableW;
  64. template< typename Char >
  65. Char* get_environment_strings();
  66. #if !defined( BOOST_NO_ANSI_APIS )
  67. template< >
  68. BOOST_FORCEINLINE char* get_environment_strings< char >()
  69. {
  70. return GetEnvironmentStrings();
  71. }
  72. BOOST_FORCEINLINE BOOL_ free_environment_strings(LPSTR_ p)
  73. {
  74. return FreeEnvironmentStringsA(p);
  75. }
  76. BOOST_FORCEINLINE DWORD_ get_environment_variable(LPCSTR_ name, LPSTR_ buffer, DWORD_ size)
  77. {
  78. return GetEnvironmentVariableA(name, buffer, size);
  79. }
  80. BOOST_FORCEINLINE BOOL_ set_environment_variable(LPCSTR_ name, LPCSTR_ value)
  81. {
  82. return SetEnvironmentVariableA(name, value);
  83. }
  84. #endif // !defined( BOOST_NO_ANSI_APIS )
  85. template< >
  86. BOOST_FORCEINLINE wchar_t* get_environment_strings< wchar_t >()
  87. {
  88. return GetEnvironmentStringsW();
  89. }
  90. BOOST_FORCEINLINE BOOL_ free_environment_strings(LPWSTR_ p)
  91. {
  92. return FreeEnvironmentStringsW(p);
  93. }
  94. BOOST_FORCEINLINE DWORD_ get_environment_variable(LPCWSTR_ name, LPWSTR_ buffer, DWORD_ size)
  95. {
  96. return GetEnvironmentVariableW(name, buffer, size);
  97. }
  98. BOOST_FORCEINLINE BOOL_ set_environment_variable(LPCWSTR_ name, LPCWSTR_ value)
  99. {
  100. return SetEnvironmentVariableW(name, value);
  101. }
  102. } // namespace winapi
  103. } // namespace boost
  104. #if defined(BOOST_WINAPI_DETAIL_GET_ENVIRONMENT_STRINGS_UNDEFINED)
  105. #if defined(_MSC_VER) || defined(__GNUC__)
  106. #pragma pop_macro("GetEnvironmentStrings")
  107. #elif defined(UNICODE)
  108. #define GetEnvironmentStrings GetEnvironmentStringsW
  109. #endif
  110. #undef BOOST_WINAPI_DETAIL_GET_ENVIRONMENT_STRINGS_UNDEFINED
  111. #endif // defined(BOOST_WINAPI_DETAIL_GET_ENVIRONMENT_STRINGS_UNDEFINED)
  112. #endif // BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  113. #endif // BOOST_WINAPI_ENVIRONMENT_HPP_INCLUDED_