I'm writing a string into QSharedMemory and after writing, I need to read it from boost program. I tried this, but didn't work.
I read the data in shared memory from the same Qt program, and I see the string.

Below is Qt program, from which I'm writing into shared memory with key ShrdMem
Qt Code:
  1. // Slot, which is invoked on click of button.
  2. void CDialog::loadString()
  3. {
  4. if(sharedMemory.isAttached())
  5. {
  6. if(!sharedMemory.detach())
  7. {
  8. lbl->setText("Unable to detach from Shared Memory");
  9. return;
  10. }
  11. }
  12.  
  13. char sString[] = "my string";
  14.  
  15. QBuffer buffer;
  16. buffer.open(QBuffer::ReadWrite);
  17.  
  18. QDataStream out(&buffer);
  19. out << sString;
  20.  
  21. int size = buffer.size();
  22. qDebug() << size;
  23.  
  24. if(!sharedMemory.create(size))
  25. {
  26. lbl->setText("Unable to create shared memory segment");
  27. qDebug() << lbl->text();
  28. }
  29. sharedMemory.lock();
  30. char *to = (char *) sharedMemory.data();
  31. const char *from = buffer.data();
  32. memcpy(to, from, qMin(sharedMemory.size(), size));
  33. sharedMemory.unlock();
  34.  
  35. char * str;
  36. QDataStream in(&buffer);
  37. sharedMemory.lock();
  38. buffer.setData((char *)sharedMemory.constData(), sharedMemory.size());
  39. buffer.open(QBuffer::ReadOnly);
  40. in >> str;
  41. sharedMemory.unlock();
  42. qDebug() << str;
  43. }
To copy to clipboard, switch view to plain text mode 
I'm trying to read the ShrdMem using Boost as shown below.
Qt Code:
  1. //NQShrdMem.cpp
  2. int main()
  3. {
  4. boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_only, "ShrdMem", boost::interprocess::read_only);
  5.  
  6. boost::interprocess::offset_t size;
  7. if (shdmem.get_size(size))
  8. std::cout << "Shared Mem Size: " << size << std::endl;
  9.  
  10. boost::interprocess::mapped_region region2(shdmem, boost::interprocess::read_only);
  11. char *i2 = static_cast<char *>(region2.get_address());
  12. std::cout << i2 << std::endl;
  13. return 0;
  14. }
To copy to clipboard, switch view to plain text mode 

I'm compiling the Boost program in cygwin, using g++:
Qt Code:
  1. g++ -I /cygdrive/c/boost/boost_1_53_0/ NQShrdMem.cpp -o NQShrdMem /cygdrive/c/boost_lib/boost/bin.v2/libs/date_time/build/gcc-mingw-4.7.2/release/link-static/threading-multi/libboost_date_time-mgw47-mt-1_53.a
To copy to clipboard, switch view to plain text mode 

Kindly help me. Thank you.