0

I am working with a large legacy C++ 6.0 code base that can not be converted to .NET. What I am trying to do is write all new functionality in C# wrap this with a COM wrapper and call that from C++. I have come across a load of articles of calling C++ from C# but very few the other way around. 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 line for any method that I declare an array in in c#

method 'ParseEquation' not emitted because of invalid return type or parameter type

Can anyone tell me how I can pass an array of a user defined class from C++ to C# Code here is the code.

c# Code

//  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 generated in C++ by importing type library

// 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
};
user655261
  • 99
  • 3
  • 12

1 Answers1

0

You import the library to be used via IDispatch interface. This allow you to expose only primitive (int, string, fload, arrays, etc) and other objects (that must implemnt IDispatch also).

So you should replace the Struct (struct IVariableCOM) whith a new IDispatch interface + implementation definition (similar as you expose 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.

Nicolae Dascalu
  • 3,425
  • 2
  • 19
  • 17