简体   繁体   English

COM接口C#编组数组

[英]COM interface c# marshalling array of arrays

I have a COM server written in C++ that should interface with a client written in C#. 我有一个用C ++编写的COM服务器,该服务器应该与用C#编写的客户端接口。 The C++ COM server exports interface functions to a structure that holds an array of structures with an array inside C ++ COM服务器将接口函数导出到一个结构,该结构包含结构数组和内部数组

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

So the structs have fixed size. 因此结构的大小固定。

The IDL descriptions are IDL描述是

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. IDL可以很好地编译,我可以在C#客户端中看到该结构。

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. 问题是,当我从C#客户端调用接口函数时,我仅在OuterStruct中的第一个InnerStruct中获取/设置值。 The second InnerStruct only holds garbage. 第二个InnerStruct仅保存垃圾。

The C# debugger shows the right structure for the OuterStruct and knows there are 2 InnerStruct inside the OuterStruct. C#调试器显示OuterStruct的正确结构,并且知道OuterStruct内部有2个InnerStruct。

In C# the declaration of the interface functions come from a server metafile, so it is not easy to change the description. 在C#中,接口函数的声明来自服务器图元文件,因此更改描述并不容易。

I have tried to set a size_is() on the in and out parameters but the MIDL compiler will not accept that. 我试图在in和out参数上设置一个size_is(),但MIDL编译器不会接受。

Can I set up some specific marshaling on the parameters or how do I solve the problem of getting the complete OuterStruct trough COM? 是否可以在参数上设置一些特定的封送处理,或者如何解决通过COM获得完整OuterStruct槽的问题?

In my IDL file I have tried to write 在我的IDL文件中,我尝试写

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 我在C ++代码中定义的位置

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. 请告诉我他们更聪明或更出色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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