Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qsharedmemory problem



  • I wont to write some bridg dll. My Code is:
    @
    /*************
    bridg.dll
    ************/
    //sharedobject.h

    #ifndef SHAREDOBJECT
    #define SHAREDOBJECT

    class ISharedObject {
    public:
    virtual void set(int data) = 0;
    virtual int get() const = 0;
    virtual ~ISharedObject() {}
    };

    class SharedObject : public ISharedObject {

    public:
    SharedObject() : data(-1) {}

    void set(int data) { this->data = data; }
    int get() const { return data; }

    private:
    int data;
    };

    #endif //SHAREDOBJECT

    //main

    #include <QSharedMemory>
    #include "sharedobject.h"
    #include "SLogger.h"

    static QSharedMemory* sharedMemory = NULL;

    const char* slogger::fileName = "bridg.log";

    extern "C" __declspec(dllexport) bool createSharedMemory(const char* key)
    {
    DEBUG() << "Key is: " << key;
    sharedMemory = new QSharedMemory(key);

    if ( !sharedMemory->create(sizeof(SharedObject)) ) return false;

    SharedObject *obj = new SharedObject();
    SharedObject sharedObj = (SharedObject) sharedMemory->data();
    DEBUG() << "Shared instance pointer: " << std::hex << obj;
    DEBUG() << "Data pointer: " << std::hex << sharedObj;

    memcpy(sharedObj, obj, sizeof(SharedObject));

    return true; //all is normal
    }

    extern "C" __declspec(dllexport) bool destroySharedMemory()
    {
    if ( sharedMemory->detach() )
    {
    delete sharedMemory;
    return true;
    }

    return false;
    }

    extern "C" __declspec(dllexport) ISharedObject* getSharedObject(const char* key)
    {
    DEBUG() << "Key is: " << key; //key is valid

    QSharedMemory sm(key);

    if ( !sm.attach() ) return NULL; //attach is ok

    ISharedObject obj = (ISharedObject)sm.data(); //<--- this local sm return invalid(another) pointer
    //ISharedObject obj = (ISharedObject)sharedMemory->data(); //<--- but global shared memory return valid pointer
    DEBUG() << "Shared instance " << std::hex << obj;

    return obj;
    }

    /*************
    wincons.exe //my test
    *************/
    #include <windows.h>
    #include <iostream>

    class ISharedObject {
    public:
    virtual void set(int data) = 0;
    virtual int get() const = 0;
    virtual ~ISharedObject() {}
    };

    typedef bool (FCreate)(const char);
    typedef bool (FDestroy)();
    typedef ISharedObject
    (FGet)(const char);

    static const char* key = "test";

    int main()
    {
    HINSTANCE dll = LoadLibrary(L"bridg.dll");

    if ( dll )
    {
    FCreate _create = (FCreate)GetProcAddress(dll, "createSharedMemory");
    FDestroy _destroy = (FDestroy)GetProcAddress(dll, "destroySharedMemory");
    FGet _get = (FGet) GetProcAddress(dll, "getSharedObject");

    if ( _create )
    {
    bool ret = _create(key);

    if ( ret )
    {
    ISharedObject* obj = _get(key);

    if ( obj ) // <---object is invalid (!!but not NULL)
    {
     //obj->set(42);
     //std::cout << "Data: " << obj->get() << std::endl;
    } else std::cout << "Can not get shared instance." << std::endl;
    

    }
    else std::cout << "Can not get create shared memory. Error: " << GetLastError() << std::endl;
    }
    else std::cout << "Can not get create function pointer. Error: " << GetLastError() << std::endl;

    FreeLibrary(dll);
    }
    else std::cout << "Can not load lybrary. Error: " << GetLastError() << std::endl;

    std::getchar();
    return 0;
    }
    @

    What i doing wrong?



  • Sorry, we're not here to debug your code. You're not even describing your symptoms...


Log in to reply