2011-11-08 69 views
1

我在VS 2010中编译和构建了一个DLL项目。我有一个添加到同一个解决方案的姊妹项目,它基本上链接到上面的DLL并需要加载它的构造函数和功能。但是,只要我尝试实例化对象,它就会给出access violation在DLL中没有实例化C++构造函数问题

在主,我这样做..

#include <iostream> 
#include "MCaromDLL.h" 

using namespace std; 
using namespace MagneticCarom; 

int main() { 
    . . . 
    MagneticCaromWrapper wrapper; 
     . . . 
} 

我 “MCaromDLL.h” 看起来是这样的:

// MCaromDLL.h 

#define NULL 0 
#define MAX_COLS 201  //Fixed based on the FEMM values 
#define MAX_ROWS MAX_COLS //Fixed based on the FEMM values 

#ifdef DLL_PROJECT 
#define DLLSPEC __declspec(dllexport) 
#else 
#define DLLSPEC __declspec(dllimport) 
#endif 

#ifndef __MCAROMDLL_H__ 
#define __MCAROMDLL_H__ 

namespace MagneticCarom 
{ 
    . . . . . . . 
    class DLLSPEC MagneticCaromWrapper 
    { 
     private: 
         //All private members here...    

     public: 
      MagneticCaromWrapper(); 

      MagneticCaromWrapper(int number); 

      virtual ~MagneticCaromWrapper(); 

         //remaining functions 
    } 
} 
#endif 

注意,我想导出整个类(虽然我试着现在也出口个别funcs,但是徒劳无功)。整个代码可以根据要求提供。

+0

不知道为什么的确切原因,但是当我尝试将对象更改为指针并包含'new'时,它开始调用构造函数而没有任何问题。有人能解释这里发生了什么吗? – Aditya369

回答

2

它总是一个问题来处理内存或结构的DLL接口。 事情可能出错:

  • 不同的运行
  • 不同allignment
  • 重载新...

,以确保它的工作原理: - 使用一个纯虚拟接口 - 使用工厂方法 - 使用删除/释放方法

IMagneticCaromWrapper* DLLSPEC FactoryMagneticCaromWrapper(); 

class IMagneticCaromWrapper 
{ 
public: 
    virtual void Release(); 
} 

与一个实现。

IMagneticCaromWrapper* DLLSPEC FactoryMagneticCaromWrapper() 
{ 
    return new MagneticCarom(); 
} 

IMagneticCaromWrapper::Release() 
{ 
    delete this; 
} 

充其量不要抛出异常跨DLL的边界。

相关问题