2011-08-15 47 views
0

我正在使用无法转换为.NET的大型传统C++ 6.0代码库。我想要做的是在C#中编写所有新功能,并用COM包装器打包并从C++中调用它。我遇到了一些C#调用C++的文章,但很少有其他方法。我有一个问题,我需要从C++传递类型变量(用户定义)的数组到C#,当我导入类型库时,我得到了C#和C++之间的交互对我来说简单的类型工作正常。下面的线,我在C#中声明数组的任何方法将结构数组从C++传递到C#

法“ParseEquation”不是因为无效的返回类型或参数类型的发射

谁能告诉我怎样才能通过用户定义的类的数组从C++到C#代码这里是代码。用C产生

C#代码

// Equation Parser 


//Events Interface 
[ComVisible(true), Guid("47C976E0-C208-4740-AC42-41212D3C34F0"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] 
public interface IEquation_EventsCOM 
{ 
} 

//COM Interface 
[ComVisible(true), Guid("3F2DE348-0BDA-4051-92B5-9B7A59FD525D")] 
public interface IEquationCOM 
{ 
    [DispId(0)] 
    string GetParserInfo(); 

    [DispId(1)] 
    float ParseEquation(IVariableCOM[] varList, string expression); 
} 

[ComVisible(true), Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E"), ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(IEquation_EventsCOM))] 
public class Equation : IEquationCOM 
{ 
    public Equation() 
    { 
    } 

    [ComVisible(true)] 
    public string GetParserInfo()//List<Variable> varList, string expression) 
    { 
     Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; 
     string name = System.Reflection.Assembly.GetExecutingAssembly().GetName().FullName; 
     return "Assemby Name: " + name + " Version: " + version.ToString(); 
    } 

    [ComVisible(true)] 
    public float ParseEquation(IVariableCOM[] varList, string expression) 
    { 
     //test return value 
     return 12.0000f; 
    } 
} 


// Equation Parser Helper Classes 

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
public struct IVariableCOM 
{ 
    public string Name; 
    public string Type; 
    public float Value; 
} 

头++通过导入类型库

// Machine generated IDispatch wrapper class(es) created with ClassWizard 
///////////////////////////////////////////////////////////////////////////// 
// IEquation_EventsCOM wrapper class 

class IEquation_EventsCOM : public COleDispatchDriver 
{ 
public: 
    IEquation_EventsCOM() {}  // Calls COleDispatchDriver default  constructor 
    IEquation_EventsCOM(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {} 
    IEquation_EventsCOM(const IEquation_EventsCOM& dispatchSrc) :  COleDispatchDriver(dispatchSrc) {} 

// Attributes 
public: 

// Operations 
public: 
}; 
///////////////////////////////////////////////////////////////////////////// 
// IEquationCOM wrapper class 

class IEquationCOM : public COleDispatchDriver 
{ 
public: 
    IEquationCOM() {}  // Calls COleDispatchDriver default constructor 
    IEquationCOM(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {} 
    IEquationCOM(const IEquationCOM& dispatchSrc) : COleDispatchDriver(dispatchSrc) {} 

// Attributes 
public: 

// Operations 
public: 
    CString GetGetParserInfo(); 
    // method 'ParseEquation' not emitted because of invalid return type or parameter  type 
}; 

回答

0

导入库经由IDispatch接口一起使用。这允许你只暴露原始数据(int,string,fload,arrays等)和其他对象(也必须实现IDispatch)。

所以你应该用新的IDispatch接口+实现定义替换Struct(struct IVariableCOM)(与暴露IEquationCOM/Equation类似)。如果你仅仅使用C++的COM对象,你可以提取idl文件,并在你的C++项目中进行编译,你可以在这里找到你需要的东西。可以访问IVariableCOM的定义。