2011-11-24 123 views
3

我已经在C#中编写了一个dll,提供了一个可供使用的类。该DLL由我编写的C程序调用。 (它是一些程序的插件,我必须用C语言编写插件的代码,但我想使用.NET的功能,因此是dll)。如何在C#dll中保留一个对象“持久化”?

在DLL中,我想开一个流和做其他的东西,应该是两次调用DLL之间持续。以下代码由私有成员Connector表示。

namespace myCSharpDll 
{ 
    // the c++ program calls this methods 
    public interface IAccess 
    { 
     double Initialize(); 
     double Timestep(double time, double[] values); 
     ... 
    } 

    // E is the beginning of another program my dll should connect to, therefore the names 
    public class EAccess : IAccess 
    { 
     // EConnector is another class I defined in the same dll 
     private EConnector Connector; 

     public double InitializeE() 
     { 
      Connector = new EPConnector(); 
     } 
     public double Timestep(double time, double[] values) 
     { 
      return Connector.Connect(); 
     } 

当我调用InitializeE()和更高版本到Timestep()时,Connector oject指向NULL。

我有什么做的,当我打电话时步()从我的C代码,我可以访问连接的创建之前实例?

我大概是在错误的方向搜索。任何提示都表示赞赏。

+2

请向我们展示您的C++代码。另外,C!= C++。 – SLaks

+0

已解决。谢谢大家。感谢SLaks - 通过我的C/C++代码向你展示给我看的错误。/Stackoverflow不让我发布答案,因为我没有收集到足够的声望点。但它会让我在8小时内。然后我发布答案。 – Teetrinker

回答

0

感谢SLaks询问我的C/C++代码。这就是问题所在。这比我想象的更简单。我把代码放在一起给你看。


我知道C和C++是不一样的,插件结构只是有点奇怪。大部分代码都是由向导生成的。我只需填写我的代码。这是一个cpp文件,但代码似乎是C.嗯,我认为这是脱离主题。

在这里,我提出了最重要的线。

// the dll is connected via COM, using the type library file that regasm generated 
#import "[path]\myCSharpDll.tlb" raw_interfaces_only 
using namespace myCSharpDll; 

static void OnActivate (IfmDocument, Widget); 

//off topic: this are the weird lines the wizard put in my way 
#ifdef __cplusplus 
extern "C" 
#endif /* __cplusplus */ 

// when the plugin is called by the host program, this function is called 
static void OnActivate (IfmDocument pDoc, Widget button) 
{ 
    InitializeIntermediate(pDoc); 
    Timestep1(...); 
} 

static void InitializeIntermediate(IfmDocument pDoc) 
{ 
    // Initialize COM. 
    HRESULT hr = CoInitialize(NULL); 
    IEPAccessPtr pIEPAccess(__uuidof(EPAccess)); 

    double result = -1; 
    pIEPAccess->InitializeEP (&result); 

    ... 
} 

static void Timestep1(...) 
{ 
    IEPAccessPtr pIEPAccess(__uuidof(EPAccess)); 

    double result = -1.1; 
    pIEPAccess->Timestep (...); 
    ... 

    // now I get a wrong result back here, because this call leads to nowhere as 
    // the connector object in the dll is void 
} 

我意识到,我请求二审与线

IEPAccessPtr pIEPAccess(__uuidof(EPAccess)); 

所以我改变了这一切指向一个单一实例,一切都很好。感谢您的意见!

0

如果我没看错你要保持整个c中的使用DLL的一个对象。如果是这种情况,请尝试类似于单例模式。

http://en.wikipedia.org/wiki/Singleton_pattern

什么单emphazises是你的一类只创建一个对象,并用它来执行所有你需要的工作。基本上,你可能需要一个功能,做这样的事情,

public class EAccess : IAccess 
    { 
     private static EConnector Connector 
     public EConnector getConnector(){ 
      if(Connector == null){ 
       Connector = new EConnector(); 
      } 
      return Connector; 
     } 
     public double Timestep(double time, double[] values) 
     { 
      return getConnector().Connect(); 
     } 

    }; 

虽然这是不使用单做事的传统方式,但我认为它仍然没有工作。我可能是错的。如果我误解了某些内容,请纠正我。

+0

请注意,这不会考虑多个线程。 –