将指针传递给dll类函数

时间:2016-11-21 21:15:16

标签: c++ c++11 dll

我需要构建一个可执行文件来加载dll @ runtime并使用多个参数调用dll中的c ++函数。我已经看到很多例子告诉我如何传递参数,但在我的情况下它不完全相同。我需要传递3个参数。

  1. 包含3个std::string元素的结构
  2. 结构指针std::mapstd::stringmap std::string为关键,结构为值
  3. std::unordered_map结构,其中std::string为关键字,std::string为值,部分bool为整数元素。
  4. dll类派生自2个基类。例如:BaseClassA - > BaseClassB - > DerivedClassA

    我已经创建了工厂函数并且正确地设置了所有__dllspec(dllexport)但是当我尝试创建DerivedClassA实例时,我得到了异常

    Unhandled exception at 0x759FA6F2 in rte.exe: Microsoft C++ exception: std::length_error at memory location 0x016FF1C4
    

    我的代码片段(dllhelp.h):

    #include <unordered_map>
    #include <cinttypes>
    
    typedef uint32_t   ID;
    
    typedef ID EVENT_UID;
    
    typedef struct {
        EVENT_UID m_unEventID;
        uint8_t  *m_ptrMemAddr;
        size_t    m_dataSize;
    }stEvent_t;
    
    typedef struct  {
        uint32_t    m_unOffset;
        uint32_t    m_unArraySize;
        std::string m_acTagName;
        std::string m_acTagValue;
        std::string m_acDataType;
    } stTagElem_t;
    
    typedef std::unordered_map<uint8_t, stTagElem_t> tagsList_t;
    
    typedef struct  {
        EVENT_UID    m_unBindedEvent;
        uint16_t     m_unMemSize;
        tagsList_t   m_stTagsList;
        std::string  m_acMapTypeName;
    } stElem_t;
    
    typedef std::unordered_map<std::string, stElem_t> ElemList_t;
    
    typedef struct  {
        std::string   m_acAppBlocName;
        std::string   m_acInstanceName;
        std::string   m_acDataMapInstanceName;
        std::string   m_acTypeName;
    } stBlock_t;
    
    typedef std::unordered_map<std::string, std::string > instanceTypeList_t;
    
    typedef struct {
        bool               m_bIsItOutput;
        uint8_t            m_unIdxToEvtClient;
        EVENT_UID          m_acBindedEvent;
        instanceTypeList_t m_acParamsList;
    } stParams_t;
    
    #ifdef EXPORTING_EXTN
    #define RTE_EXTN __declspec(dllexport)
    #else
    #define RTE_EXTN __declspec(dllimport)
    #endif
    
    class RTE_EXTN BaseA {
    public:
      BaseA(){}
      virtual ~BaseA(){}
      void rxdEvent(stEvent_t pa_stEvent){ onRxdEvent(pa_stEvent);}
    protected:
      virtual void onRxdEvent(stEvent_t pa_stEvent); 
    };
    
    class RTE_EXTN BaseB : public BaseA {
    public:
      BaseB(stParams_t pa_stParams){;}
      virtual ~BaseB(){}
      void sendOutput(size_t dataSize);
      virtual int initialise() {return -1;}
      virtual void onRxdEvent(stEvent_t pa_stEvent) {};
      stBlock_t m_stBlock;
      stElem_t  *m_stElem;
    protected:
      stEvent_t m_stEvent;
      stParams_t m_stParams;
    };
    
    class RTE_EXTN DerivedClassA : public BaseB {
    public:
      DerivedClassA(stBlock_t pa_stBlock, stElem_t *pa_stElem, stParams_t pa_stParams) : BaseB(pa_stParams){
        m_stElem = pa_stElem;
        m_stBlock = pa_stBlock;
      }
      int initialise(){return 0;}
      void onRxdEvent(stEvent_t pa_stEvent);
    };
    
    extern "C" {
      RTE_EXTN BaseB* CreateDerivedObj(stBlock_t pa_stBlock, stElem_t *pa_stElem, stParams_t pa_stParams);
    };
    
    typedef BaseB* (*CreateDrvObjType_t) (stBlock_t pa_stBlock, stElem_t *pa_stElem, stParams_t pa_stParams);
    

    我的dllmain.cpp:

    #include "dllhelp.h"
    BaseA* CreateDerivedObject(stBlock_t pa_stBlock, stElem_t *pa_stElem, stParams_t pa_stParams)
    {       
        return new DerivedClassA(pa_stBlock, pa_stElem, pa_stParams);
    }
    

    我的主要可执行文件:

    stBlock_t   m_siInfo;
    stElem_t    *pa_stElem;
    stParams_t  m_siParams;
    //the above structures are initialized and updated with valid data..
    
    HINSTANCE hGetProcIDDLL_derived;
    typedef BaseB* (*CreateDrvObjType_t) (stBlock_t pa_stBlock, stElem_t *pa_stElem, stParams_t pa_stParams);
    hGetProcIDDLL_derived = LoadLibrary(TEXT("dllhelp.dll")); 
    CreateDrvObjType_t oExtension = reinterpret_cast<CreateDrvObjType_t>(GetProcAddress(hGetProcIDDLL_derived, "CreateDerivedObj"));
    BaseB* paDrvObj = oExtension(m_siInfo,pa_stElem,m_siParams);
    

    尝试调试模式时,我能够看到所有结构都填充了有效元素。但是在dllmain.cpp上进行调试时,我无法在结构上看到任何有效数据。有些指向空地址,有些指向无效数据,有些指出&#34;错误读取字符串&#34;。

    注意:我在主可执行文件中不使用dllhelp.h文件。相反,我已经在主文件中声明了所有结构。

0 个答案:

没有答案