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
// Slot, which is invoked on click of button.
void CDialog::loadString()
{
if(sharedMemory.isAttached())
{
if(!sharedMemory.detach())
{
lbl->setText("Unable to detach from Shared Memory");
return;
}
}
char sString[] = "my string";
out << sString;
int size = buffer.size();
qDebug() << size;
if(!sharedMemory.create(size))
{
lbl->setText("Unable to create shared memory segment");
qDebug() << lbl->text();
}
sharedMemory.lock();
char *to = (char *) sharedMemory.data();
const char *from = buffer.data();
memcpy(to, from, qMin(sharedMemory.size(), size));
sharedMemory.unlock();
char * str;
sharedMemory.lock();
buffer.setData((char *)sharedMemory.constData(), sharedMemory.size());
in >> str;
sharedMemory.unlock();
qDebug() << str;
}
// Slot, which is invoked on click of button.
void CDialog::loadString()
{
if(sharedMemory.isAttached())
{
if(!sharedMemory.detach())
{
lbl->setText("Unable to detach from Shared Memory");
return;
}
}
char sString[] = "my string";
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);
out << sString;
int size = buffer.size();
qDebug() << size;
if(!sharedMemory.create(size))
{
lbl->setText("Unable to create shared memory segment");
qDebug() << lbl->text();
}
sharedMemory.lock();
char *to = (char *) sharedMemory.data();
const char *from = buffer.data();
memcpy(to, from, qMin(sharedMemory.size(), size));
sharedMemory.unlock();
char * str;
QDataStream in(&buffer);
sharedMemory.lock();
buffer.setData((char *)sharedMemory.constData(), sharedMemory.size());
buffer.open(QBuffer::ReadOnly);
in >> str;
sharedMemory.unlock();
qDebug() << str;
}
To copy to clipboard, switch view to plain text mode
I'm trying to read the ShrdMem using Boost as shown below.
//NQShrdMem.cpp
int main()
{
boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_only, "ShrdMem", boost::interprocess::read_only);
boost::interprocess::offset_t size;
if (shdmem.get_size(size))
std::cout << "Shared Mem Size: " << size << std::endl;
boost::interprocess::mapped_region region2(shdmem, boost::interprocess::read_only);
char *i2 = static_cast<char *>(region2.get_address());
std::cout << i2 << std::endl;
return 0;
}
//NQShrdMem.cpp
int main()
{
boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_only, "ShrdMem", boost::interprocess::read_only);
boost::interprocess::offset_t size;
if (shdmem.get_size(size))
std::cout << "Shared Mem Size: " << size << std::endl;
boost::interprocess::mapped_region region2(shdmem, boost::interprocess::read_only);
char *i2 = static_cast<char *>(region2.get_address());
std::cout << i2 << std::endl;
return 0;
}
To copy to clipboard, switch view to plain text mode
I'm compiling the Boost program in cygwin, using g++:
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
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.
Bookmarks