【问题标题】:boost::interprocess::managed_shared_memory() open_only fails on XPboost::interprocess::managed_shared_memory() open_only 在 XP 上失败
【发布时间】:2015-11-23 11:54:03
【问题描述】:

我有两个简单的应用程序,它们使用来自boost_1_55_0 的共享内存。
BoostCreate.exe 创建共享内存。
BoostOpen.exe 打开创建的共享内存。
我用 VS2008 和它们在 Windows7/8.1 上工作,但在 Windows XP 上BoostOpen.exe 无法打开共享内存。
这是BoostCreate.exe 代码:

using namespace boost::interprocess;
std::string shMemName("MySharedMem");
managed_shared_memory shMem = managed_shared_memory(create_only, shMemName.c_str(), 1000);

这是BoostOpen.exe 代码:

using namespace boost::interprocess;
std::string shMemName("MySharedMem");
try
{
  managed_shared_memory shMem = managed_shared_memory(open_only, shMemName.c_str());
}
catch(boost::interprocess::interprocess_exception& e)
{
    printf("Error opening %s\n", e.what());
}

例外是:The system cannot find the file specified

尝试boost_1_59_0 我得到一些链接器错误:

error LNK2001: unresolved external symbol "public: void __thiscall boost::container::container_detail::has_member_function_named_construct<class boost::container::new_allocator<char> >::BaseMixin::construct(void)" (?construct@BaseMixin@?$has_member_function_named_construct@V?$new_allocator@D@container@boost@@@container_detail@container@boost@@QAEXXZ)

【问题讨论】:

  • 链接器错误可以通过构建调试版本或禁用优化来解决,它出现了。试试看 Boost 1_59 上的行为是什么
  • 您是否在同一个session 中运行客户端?权限和命名空间可能导致地图不可见/不可访问
  • 是的,使用本机 Win32 CreateFileMappingOpenFileMapping,我可以创建和打开相同的共享内存。不幸的是,我在这里使用managed_shared_memory;;construct&lt;T&gt;,并且需要重写大量代码才能使用本机 API。
  • Boost 1_55Boost 1_59 的结果相同
  • 你能追溯到哪些 Win32 API 失败了吗?

标签: c++ windows boost


【解决方案1】:

BoostOpen.exeCreateFileA api 调用中失败,返回 -1 作为句柄值。
我注意到在 Windows7 上,boost 共享内存名称是:

C:\ProgramData/boost_interprocess/1447248616/MySharedMem

在 XP 中是:

C:\Documents and Settings\All Users\Application Data/boost_interprocess//MySharedMem

看起来// 之间缺少时间戳。
我发现get_bootstamp() (tmp_dir_helpers.hpp) 在 XP 上返回一个空的 bootstamp.stamp 字符串。
为了避免添加这个空时间戳,我在 \boost_1_55_0\boost\interprocess\detail\workaround.hpp 中注释了第 19 行

//#define BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME

现在有了名字:

C:\Documents and Settings\All Users\Application Data/boost_interprocess/MySharedMem

它适用于 Windows7 和 XP。
另一种解决方案是保留第 19 行并取消注释第 21 行:

#define BOOST_INTERPROCESS_BOOTSTAMP_IS_LASTBOOTUPTIME

【讨论】:

    猜你喜欢
    • 2019-01-23
    • 2012-05-20
    • 2014-05-06
    • 1970-01-01
    • 2021-05-19
    • 2017-11-02
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多