[英]Passing struct array from C++ to C#
我正在使用無法轉換為 .NET 的大型遺留 C++ 6.0 代碼庫。 我想做的是在 C# 中編寫所有新功能,用 COM 包裝器包裝它,然后從 C++ 調用它。 我遇到了大量從 C# 調用 C++ 的文章,但反過來卻很少。 The interaction between C# and C++ is working fine for me for simple types, however I have hit a problem, I need to pass an array of type Variable(user defined) from C++ to C#, when I import the type library I get the following我在 c# 中聲明數組的任何方法的行
由於返回類型或參數類型無效,未發出方法“ParseEquation”
誰能告訴我如何將用戶定義的 class 數組從 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;
}
header 通過導入類型庫在 C++ 中生成
// 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
};
您通過 IDispatch 接口導入要使用的庫。 這允許您僅公開原始(int、string、fload、arrays 等)和其他對象(也必須實現 IDispatch)。
因此,您應該用新的 IDispatch 接口 + 實現定義替換 Struct (struct IVariableCOM)(類似於您公開 IEquationCOM/Equation)。
A low level solution (you should known the COM rules: memory management, etc): If you are using the COM objects only from c++, you can extract the idl file, and compile it on your c++ project where you can access the IVariableCOM definition .
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.