i tried that ,but couldn't write even a simple text.
here below is my code.
app1 which creates and writes into the shared memory.
qbuffer.write(qPrintable(str));
int size = qbuffer.size();
QSharedMemory shm("mykey");
shm.create(4096);
shm.lock();
char *to = (char*)shm.data();
const char *from = qbuffer.data().data();
memcpy(to, from, qMin(shm.size(), size));
shm.unlock();
shm.detach();
QBuffer qbuffer;
QString str="hi qt";
qbuffer.write(qPrintable(str));
int size = qbuffer.size();
QSharedMemory shm("mykey");
shm.create(4096);
shm.lock();
qbuffer.open(QBuffer::ReadWrite);
char *to = (char*)shm.data();
const char *from = qbuffer.data().data();
memcpy(to, from, qMin(shm.size(), size));
shm.unlock();
shm.detach();
To copy to clipboard, switch view to plain text mode
app2 which reads
char ch[6];
qint64 si=9;
QSharedMemory shm("mykey");
if(!shm.attach())
qFatal("Couldn't attach to shared memory");
shm.lock();
qbuffer.setData((char*)shm.constData(), shm.size());
shm.unlock();
qbuffer.seek(0);
qbuffer.read(ch,si);
cout<<ch;
QBuffer qbuffer;
char ch[6];
qint64 si=9;
QSharedMemory shm("mykey");
if(!shm.attach())
qFatal("Couldn't attach to shared memory");
shm.lock();
qbuffer.open(QBuffer::ReadOnly);
qbuffer.setData((char*)shm.constData(), shm.size());
shm.unlock();
qbuffer.seek(0);
qbuffer.read(ch,si);
cout<<ch;
To copy to clipboard, switch view to plain text mode
whats wrong with the code???
Bookmarks