I'm trying to get boost::interprocess to share memory between 32 bit and 64 bit processes. This bug tracker entry suggests that this might be possible in Boost 1.49, which is what I use.
As a test I tried sharing an unsigned int. It's a simple Qt application with two buttons.
#define SHARED_MEMORY_NAME "My shared memory"
#define SHARED_VAR_NAME "testVar"
namespace bip = boost::interprocess;
void on_createMemButton_clicked()
{
std::cout << "sizeof(unsigned int): " << sizeof(unsigned int) << std::endl;
bip::shared_memory_object::remove(SHARED_MEMORY_NAME);
bip::managed_shared_memory mem(bip::create_only, SHARED_MEMORY_NAME, 12345);
mem.construct<unsigned int>(SHARED_VAR_NAME)(42);
std::cout << "Created shared memory " << SHARED_MEMORY_NAME << std::endl;
}
void on_accessMemButton_clicked()
{
try
{
std::cout << "sizeof(unsigned int): " << sizeof(unsigned int) << std::endl;
bip::managed_shared_memory mem(bip::open_only, SHARED_MEMORY_NAME);
std::pair<unsigned int*, size_t> p = mem.find<unsigned int>(SHARED_VAR_NAME);
std::cout<< "got " << p.second << " numbers " << std::endl;
if (p.second > 0)
std::cout << "first number is: " << *p.first << std::endl;
bip::shared_memory_object::remove(SHARED_MEMORY_NAME);
}
catch (bip::interprocess_exception e)
{
std::cout << "Shared mem " << SHARED_MEMORY_NAME << " not found" << std::endl;
}
}
If I create or access the shared memory from processes with the same bitness it works without problems. But if I create the memory in a 64 bit process, and read from a 32 bit process, the process enters the managed_shared_memory::find() function and never comes back. If I try it the other way round, the 64 bit process fails in managed_shared_memory::find() again, this time with an access violation. Using managed_windows_shared_memory yielded the same results.
Is there any way to make this work?
uint32_tinstead ofunsigned intin case of cross-architecture code, that could be a problem depending on your compiler. I am using boost ipc too for x86 64<->32 communication on windows using the visual c++ 10.0 compiler, with boost 1.50 and it works like a charm. - ch0keeCreateFileMapping,CreateMutex(NULL, FALSE, ...)). - Andreas Haferburg