Pergunta

Let me begin by stating, I'm not a COM developer. I know standard C++, C#, and Java.

I have a C# library that will be called from Managed C++. I've added C++ classes using Visual Studio 2010 --> MFC Class from TypeLib. The autogenerated C++ class doesn't compile though. It is obviously missing a return statement. How do I fix this? I seriously appreciate any help.

C# Class from Library

[Serializable]
[
       ClassInterface(ClassInterfaceType.AutoDual),
       ProgId("Response")
]
public class Response
{
    public static readonly int NUM_DATA = 6;

    public Response()
    {
        data = new Data[NUM_DATA];
        for (int i = 0; i < NUM_DATA; ++i)
        {
            data[i] = new Data();
        }
    }

    private Data[] data;
    public Data[] Data
    {
        [return: MarshalAs(UnmanagedType.SafeArray)]
        get
        {
            return data;
        }
    }
}

Managed C++ Generated Class

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

    // _Response methods
public:
    SAFEARRAY * get_Data()
    {
        InvokeHelper(0x60020004, DISPATCH_PROPERTYGET, VT_EMPTY, NULL, NULL);
    }
}
Foi útil?

Solução

It seems there are threecommon solutions to this problem. The first is to put the return array as a parameter to the function. So like this:

CustomData[] getCustomDataArray();
void getCustomDataArray(out CustomData[]);

The second option is to write a function for accessing the array rather than using the whole thing.

CustomData getCustomDataAt(int index);
void setCustomDataAt(int index, CustomData data);

The final option is to change the MarshalAs attribute. I will be using the first option as it seems to be the most commonly accepted solution. Hope this helps others.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top