2011-08-15 45 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的定義。