Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qsharedmemory problem
Forum Updated to NodeBB v4.3 + New Features

Qsharedmemory problem

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stima_ua
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved