windows_shared_dir_func.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/interprocess/detail/config_begin.hpp>
  11. #include <boost/interprocess/detail/workaround.hpp>
  12. #ifdef BOOST_INTERPROCESS_WINDOWS
  13. #define BOOST_INTERPROCESS_WINDOWS
  14. //Force user-defined get_shared_dir
  15. #define BOOST_INTERPROCESS_SHARED_DIR_FUNC
  16. #include <boost/interprocess/detail/shared_dir_helpers.hpp>
  17. #include <string>
  18. #include "get_process_id_name.hpp"
  19. namespace boost {
  20. namespace interprocess {
  21. namespace ipcdetail {
  22. static bool dir_created = false;
  23. inline void get_shared_dir(std::string &shared_dir)
  24. {
  25. shared_dir = boost::interprocess::ipcdetail::get_temporary_path();
  26. shared_dir += "/boostipctest_";
  27. shared_dir += boost::interprocess::test::get_process_id_name();
  28. if(!dir_created)
  29. ipcdetail::create_directory(shared_dir.c_str());
  30. dir_created = true;
  31. }
  32. }}} //namespace boost::interprocess::ipcdetail
  33. #include <boost/interprocess/shared_memory_object.hpp>
  34. #include <iostream>
  35. int main ()
  36. {
  37. using namespace boost::interprocess;
  38. const char *const shm_name = "test_shm";
  39. std::string shared_dir;
  40. ipcdetail::get_shared_dir(shared_dir);
  41. std::string shm_path(shared_dir);
  42. shm_path += shm_name;
  43. int ret = 0;
  44. shared_memory_object::remove(shm_name);
  45. {
  46. shared_memory_object shm(create_only, shm_name, read_write);
  47. shm_path += shm_name;
  48. int ret = ipcdetail::invalid_file() == ipcdetail::open_existing_file(shm_path.c_str(), read_only) ?
  49. 1 : 0;
  50. if(ret)
  51. {
  52. std::cerr << "Error opening user get_shared_dir()/shm file" << std::endl;
  53. }
  54. }
  55. shared_memory_object::remove(shm_name);
  56. ipcdetail::remove_directory(shared_dir.c_str());
  57. return ret;
  58. }
  59. #else
  60. int main()
  61. {
  62. return 0;
  63. }
  64. #endif //#ifdef BOOST_INTERPROCESS_WINDOWS
  65. #include <boost/interprocess/detail/config_end.hpp>