Boost Interprocess共享内存 - 只在一个应用程序中读取不可能

时间:2014-11-17 16:29:04

标签: c++ qt boost

我有三个申请。

  1. Feeder - 创建一个boost::interprocess共享内存对象并填充它。 Feeder是基于Qt的。
  2. Gui - 有很多按钮和文本框,可以从中创建结构化配置。 Gui是基于Qt的。
  3. User - 读取由Gui创建的配置并连接到由Feeder填充的共享内存,然后执行计算。 User是一个控制台C ++,没有Qt。
  4. 问题是一旦Feeder填充共享内存,Gui CAN就可以访问它,而User无法GuiUser使用用于读取共享内存的相同代码。两年前,使用Qt 4.X这很好用,今天我在Qt 5.1.1。 - 我不知道是否与Qt版本有关联。

    仅为了说明,代码片段是

    Feeder

    this->mem = new shared_memory_object(create_only, "tickChartSize",  read_write);
    this->mem ->truncate(sizeof(unsigned int));
    mapped_region region( *this->mem, read_write);
    *((unsigned int))region.get_address()) = this->size();          
    

    Gui + User

    shared_memory_object *mem = new shared_memory_object(open_only, "tickChartSize", read_only);
    this->u = (void*)new mapped_region( *mem, read_only );  // works in Gui, throws in User
    

    我进入了强化代码,发现了GuiUser之间的差异,就在User - 在{_ 1楼}中的第459行投放

    之前
     //Obtain mapping size if user provides 0 size
          if(size == 0){
             offset_t mapping_size;
    THIS --> if(!winapi::get_file_mapping_size(native_mapping_handle, mapping_size)){
                error_info err = winapi::get_last_error();
                throw interprocess_exception(err);
             }
             //This can throw
             priv_size_from_mapping_size(mapping_size, offset, page_offset, size);
          }
    

    winapi::get_file_mapping_size来电完成后,mapping_size中的Gui为8,而User中的err -3689348814741910324 。有关错误的唯一信息是err.m_ec = system_error(1)结构,但是{{1}},即此消息没有提供太多帮助。

    构建信息:

    • Qt 5.1.1
    • boost 1.54.0
    • Windows 7 x64
    • Visual Studio 2012
    • 所有应用都编译为x64

    非常感谢有关此主题的任何帮助。

    丹尼尔

    编辑 - 解决方案

    尽管我最初的VS项目没有任何明显的错误,但重新创建项目使得这个错误的行为消失了。感谢所有观众。

1 个答案:

答案 0 :(得分:1)

win32实现继续使用NtQuerySection来获取映射的大小:

inline bool get_file_mapping_size(void *file_mapping_hnd, __int64 &size)
{
   NtQuerySection_t pNtQuerySection =
      (NtQuerySection_t)dll_func::get(dll_func::NtQuerySection);
   //Obtain file name
   interprocess_section_basic_information info;
   unsigned long ntstatus =
      pNtQuerySection(file_mapping_hnd, section_basic_information, &info, sizeof(info), 0);
   size = info.section_size;
   return !ntstatus;
}

Windows上的错误代码1表示ERROR_INVALID_FUNCTION。这意味着此 undocumented function from ntdll.dll 无法访问,或者无法与传递的特定句柄file_mapping_hnd一起使用。

也许,有了这些信息,您可以在Boost邮件列表中找到原因或询问它