简体   繁体   English

将object的c#数组传递给COM接口方法

[英]Passing a c# array of object to COM interface method

I should pass a pointer of object array to a COM interface with the following IDL and C++ definitions: C++ code:我应该将 object 数组的指针传递给具有以下 IDL 和 C++ 定义的 COM 接口: ZF6F87C3FDCF8B31C2FC907

UpdateItem( LONG lID, LONG lNumOfFields, FieldIdEnum* pFields, VARIANT* pvValues )

IDL:身份证:

HRESULT UpdateItem( [in] LONG lID, [in] LONG lNumOfFields, [in, size_is(lNumOfFields)] FieldIdEnum* pFields, [in, size_is(lNumOfFields)] VARIANT* pvValues );

tlbimp generated C# code: tlbimp 生成的 C# 代码:

UpdateItem([In] int lID, [In] int lNumOfFields, [In] ref FieldIdEnum pFields, [In][MarshalAs(UnmanagedType.Struct)] ref object pvValues);

... and I try to call with: ...我试着打电话给:

FieldIdEnum[] fieldIDs = { FieldIdEnum.fi1ID, FieldIdEnum.fi2At };
object[] values = { 3, DateTime.UtcNow };
mi.UpdateItem(1, 2, ref fieldIDs[0], ref values[0]);

I get this interface from my system as result of login and authentication process... The UpdateItem method is a public member of that interface.作为登录和身份验证过程的结果,我从我的系统中获得了这个接口...... UpdateItem 方法是该接口的公共成员。

I want to pass two or more field and value pairs (in same time) by the two array.我想通过两个数组传递两个或多个字段和值对(同时)。 The field array always is passed correctly to C++ code.字段数组始终正确传递给 C++ 代码。 I have debugged C++ code and the "FieldIdEnum* pFields" represents the first element of an integer array and the 2nd element (pFields 1 ) is correct too.我已经调试了 C++ 代码,“FieldIdEnum* pFields”代表 integer 数组的第一个元素,第二个元素(pFields 1 )也是正确的。 The pField[] is a c-style array. pField[] 是一个 c 风格的数组。

But the 2nd array is a variant array with various element type in this case an integer and a date time.但是第二个数组是具有各种元素类型的变量数组,在这种情况下是 integer 和日期时间。 This appears as a SafeArray at c++ side instead of c_style array.这在 c++ 侧显示为 SafeArray,而不是 c_style 数组。 I have checked this 2nd array too, it is a simple variant array if the method calling starts from another C code, but C# tries to marshal it to safearray.我也检查了第二个数组,如果方法调用从另一个 C 代码开始,它是一个简单的变体数组,但 C# 尝试将其编组到安全数组。

I have changed the C# definition to我已将 C# 定义更改为

UpdateItem([In] int lID, [In] int lNumOfFields, [In] ref FieldIdEnum pFields, [In] ref object pvValues);

It does not work I always get an exception like "Value does not fall within the expected range."它不起作用我总是得到一个异常,比如“值不在预期范围内”。 "System.Exception {System.ArgumentException}" “System.Exception {System.ArgumentException}”

I have tried to use/marshal various managed/unmanaged types and Intptr, did not work.我尝试使用/编组各种托管/非托管类型和 Intptr,但没有成功。

I have found an other topic with same/similar problem, I could not make it work.我发现了另一个具有相同/相似问题的主题,但我无法使其工作。

How should I pass a pointer of object array to COM interface?我应该如何将 object 数组的指针传递给 COM 接口?

I have found the solution.我找到了解决方案。 I have redefined the interface method:我重新定义了接口方法:

        void UpdateItem([In] int lID, [In] int lNumOfFields, [In] ref FieldIdEnum pFields, [In][MarshalAs(UnmanagedType.LPArray)] object[] pvValues);

... and call this as: ...并将其称为:

            FieldIdEnum[] updateFieldIDs = { ..., ... };
            object[] values = { ..., ... };
            
            mi.UpdateItem(id, updateFieldIDs.Length, ref updateFieldIDs[0], values);

Thank you for guidance.感谢您的指导。

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

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