简体   繁体   中英

COM interface c# marshalling array of arrays

I have a COM server written in C++ that should interface with a client written in C#. The C++ COM server exports interface functions to a structure that holds an array of structures with an array inside

struct InnerStruct  { int innerArray[ 100 ]; }
struct OuterStruct  { int dummy; InnerStruct outerArray[ 2 ]; }

So the structs have fixed size.

The IDL descriptions are

HRESULT SetStruct( [in] const OuterStruct* par, [out, retval] int code );
HRESULT GetStruct( [out] OuterStruct* par, [out, retval] int code );

The IDL compile fine and I can see the structure in the C# client.

The problem is that when I call the interface functions from the C# client I only get/set the values in the first InnerStruct in the OuterStruct. The second InnerStruct only holds garbage.

The C# debugger shows the right structure for the OuterStruct and knows there are 2 InnerStruct inside the OuterStruct.

In C# the declaration of the interface functions come from a server metafile, so it is not easy to change the description.

I have tried to set a size_is() on the in and out parameters but the MIDL compiler will not accept that.

Can I set up some specific marshaling on the parameters or how do I solve the problem of getting the complete OuterStruct trough COM?

In my IDL file I have tried to write

typedef [transmit_as(OuterStructAliasType)] OuterStruct* HelpType;

and keep the other declarations as

HRESULT SetStruct( [in] const OuterStruct* par, [out, retval] int code );
HRESULT GetStruct( [out] OuterStruct* par, [out, retval] int code );

where in my C++ code I have defined

typedef struct OuterStructAliasType
{
    char    dummy[ sizeof( OuterStruct )];
} OuterStructAliasType;

This seems to work. There maybe are other ways to the trick. Please tell me it they are smarter or better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM