简体   繁体   English

如何将SAFEARRAY从C#传递到COM?

[英]How to pass a SAFEARRAY from C# to COM?

I have an ATL COM Server, where the method for the interface is 我有一个ATL COM服务器,其中接口的方法是

CVivsBasic::UpdateSwitchPlan(BSTR plan_name, SAFEARRAY* plan)

And the IDL for this function looks like 这个功能的IDL看起来像

typedef struct  
{   
    LONG time_to_play;
    BSTR ecportid;
} SwitchPlanItem;
HRESULT UpdateSwitchPlan([in] BSTR plan_name, [in] SAFEARRAY(SwitchPlanItem) plan) ;    

I tried to call it from C# like this: 我试图这样从C#调用它:

        internal void UpdateSwitch(string plan_name, string ecportid)
    {
        SwitchPlanItem sp1;
        sp1.time_to_play = 33;
        sp1.ecportid = ecportid;

        SwitchPlanItem sp2;
        sp2.time_to_play = 33;
        sp2.ecportid = ecportid;

        SwitchPlanItem[] sps = { sp1, sp2 };

        sdk.UpdateSwitchPlan(plan_name, sps);
    }

But it crash. 但是它崩溃了。 What is the correct way to pass a SAFEARRAY from C# to COM? 将SAFEARRAY从C#传递到COM的正确方法是什么?

I think the problem here is that you're using a SAFEARRAY of user defined types (UDT), SAFEARRAY s of VARIANT , BSTR and IUnknown work out of the box but for UDT s you need to help the marshaller along. 我觉得这里的问题是,您使用的是SAFEARRAY用户定义类型(UDT), SAFEARRAYVARIANTBSTRIUnknown工作开箱即用,但对于UDT就是你需要帮助一起编组。 See this article in MSDN regarding Passing Safearray of UDTs . 请参阅MSDN中有关传递UDT的Safearray的文章。

I think the answer to this question is similar to this one: COM - [in] parameter as SAFEARRAY(STRUCT) 我认为这个问题的答案与此类似: COM-[in]参数为SAFEARRAY(STRUCT)

Basically, the C# client that is using an interface where SAFEARRAY(STRUCT) are passed in, has to define Embed Interop Types = False on the imported COM server reference properties. 基本上,使用传入SAFEARRAY(STRUCT)的接口的C#客户端必须在导入的COM服务器引用属性上定义Embed Interop Types = False

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

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