environment_win.ipp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // process/this_process/detail/environment_win.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2021 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_PROCESS_V2_DETAIL_IMPL_ENVIRONMENT_WIN_IPP
  11. #define BOOST_PROCESS_V2_DETAIL_IMPL_ENVIRONMENT_WIN_IPP
  12. #include <boost/process/v2/detail/config.hpp>
  13. #include <boost/process/v2/detail/environment_win.hpp>
  14. #include <boost/process/v2/detail/impl/environment.ipp>
  15. #include <boost/process/v2/detail/last_error.hpp>
  16. #include <algorithm>
  17. #include <cwctype>
  18. #include <cstring>
  19. #include <shellapi.h>
  20. #include <boost/process/v2/cstring_ref.hpp>
  21. #include <boost/process/v2/error.hpp>
  22. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  23. namespace environment
  24. {
  25. namespace detail
  26. {
  27. std::basic_string<char_type, value_char_traits<char_type>> get(
  28. basic_cstring_ref<char_type, key_char_traits<char_type>> key,
  29. error_code & ec)
  30. {
  31. std::basic_string<char_type, value_char_traits<char_type>> buf;
  32. std::size_t size = 0u;
  33. do
  34. {
  35. buf.resize(buf.size() + 4096);
  36. size = ::GetEnvironmentVariableW(key.c_str(), &buf.front(), static_cast<DWORD>(buf.size()));
  37. }
  38. while (size == buf.size());
  39. buf.resize(size);
  40. if (buf.size() == 0)
  41. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  42. return buf;
  43. }
  44. void set(basic_cstring_ref<char_type, key_char_traits<char_type>> key,
  45. basic_cstring_ref<char_type, value_char_traits<char_type>> value,
  46. error_code & ec)
  47. {
  48. if (!::SetEnvironmentVariableW(key.c_str(), value.c_str()))
  49. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  50. }
  51. void unset(basic_cstring_ref<char_type, key_char_traits<char_type>> key,
  52. error_code & ec)
  53. {
  54. if (!::SetEnvironmentVariableW(key.c_str(), nullptr))
  55. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  56. }
  57. std::basic_string<char, value_char_traits<char>> get(
  58. basic_cstring_ref<char, key_char_traits<char>> key,
  59. error_code & ec)
  60. {
  61. std::basic_string<char, value_char_traits<char>> buf;
  62. std::size_t size = 0u;
  63. do
  64. {
  65. buf.resize(buf.size() + 4096);
  66. size = ::GetEnvironmentVariableA(key.c_str(), &buf.front(), static_cast<DWORD>(buf.size()));
  67. }
  68. while (size == buf.size());
  69. buf.resize(size);
  70. if (buf.size() == 0)
  71. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  72. return buf;
  73. }
  74. void set(basic_cstring_ref<char, key_char_traits<char>> key,
  75. basic_cstring_ref<char, value_char_traits<char>> value,
  76. error_code & ec)
  77. {
  78. if (!::SetEnvironmentVariableA(key.c_str(), value.c_str()))
  79. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  80. }
  81. void unset(basic_cstring_ref<char, key_char_traits<char>> key,
  82. error_code & ec)
  83. {
  84. if (!::SetEnvironmentVariableA(key.c_str(), nullptr))
  85. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  86. }
  87. native_handle_type load_native_handle() { return ::GetEnvironmentStringsW(); }
  88. void native_handle_deleter::operator()(native_handle_type nh) const
  89. {
  90. ::FreeEnvironmentStringsW(nh);
  91. }
  92. native_iterator next(native_iterator nh)
  93. {
  94. while (*nh != L'\0')
  95. nh++;
  96. return ++nh;
  97. }
  98. native_iterator find_end(native_handle_type nh)
  99. {
  100. while ((*nh != L'\0') || (*std::next(nh) != L'\0'))
  101. nh++;
  102. return ++nh;
  103. }
  104. bool is_executable(const filesystem::path & pth, error_code & ec)
  105. {
  106. return filesystem::is_regular_file(pth, ec) && SHGetFileInfoW(pth.native().c_str(), 0,0,0, SHGFI_EXETYPE);
  107. }
  108. }
  109. }
  110. BOOST_PROCESS_V2_END_NAMESPACE
  111. #endif //BOOST_PROCESS_V2_DETAIL_IMPL_ENVIRONMENT_WIN_IPP