2011-11-08 66 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的邊界。

相關問題