QSharedMemory works on win xp, but not on win7



  • Hi,
    In my project I use QSharedMemory between two processes,

    Process "A" create and write into the shared memory.
    Process "B" read from the memory. (process "B" is also a non-gui windows service.)

    When I run on win xp sp3 32 bit it's works fine!

    BUT, when I try to run on win7 64 bit,
    the attach() fails with the error of : "handle doesn't exists" .

    I tried to add to the shared memory name the prefix of "Global" as described on
    "this discussion--":https://bugreports.qt-project.org/browse/QTBUG-5123
    And it still dosen't work on win7.

    My code:

    Process "A"

    @
    void SharedMemory::write(const QString &key, const QString &msg)
    {
    sharedMemory.setKey(key);

    if (sharedMemory.isAttached())
        detach();
    
    QBuffer buffer;
    buffer.open(QBuffer::ReadWrite);
    QDataStream out(&buffer);
    out << msg;
    int size = buffer.size();
    
    if ( !sharedMemory.create(size) ) {
        qDebug("Unable to create shared memory segment.");
        return;
    }
    sharedMemory.lock();
    char *to = (char*)sharedMemory.data();
    const char *from = buffer.data().data();
    memcpy(to, from, qMin(sharedMemory.size(), size));
    sharedMemory.unlock();
    

    }

    void SharedMemory::detach()
    {
    if (!sharedMemory.detach())
    qDebug("Unable to detach from shared memory.");
    }
    @

    Process "B" - fails to attach()
    @
    QString InteractiveService::read_from_memory(const QString &key)
    {
    QFile file("C:\test\debugSharedMem.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QString debug_str;
    QString recive;

    sharedMemory.setKey(key);
    if (!sharedMemory.attach()) {
        // If an attempt of reading from the shared memory before data is written
        debug_str = "Cannot attach to shared memory to update! ERROR: ";
        debug_str.append(sharedMemory.errorString());
        file.write(debug_str.toAscii());
        file.close();
        return recive;
    }
    QBuffer buffer;
    QDataStream in(&buffer);
    
    sharedMemory.lock();
    buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
    buffer.open(QBuffer::ReadOnly);
    in >> recive;
    sharedMemory.unlock();
    
    sharedMemory.detach();
    
    debug_str.append("Shared mem is open");
    file.write(debug_str.toAscii());
    file.close();
    return recive;
    

    }
    @

    Can't understand why it's working on one windows OS and not in the other.

    Please help.

    I'm using
    QT 4.7.0 on win xp sp3 32 bit.



  • I found that if I use the 2 process as regular windows app.
    The IPC works fine on win7 and win xp.

    The problem is when I'm trying to attach the shared memory from the service app on win7.

    Help is needed....


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.