如何将SAFEARRAY从C#传递到COM? [英] How to pass a SAFEARRAY from C# to COM?

查看:619
本文介绍了如何将SAFEARRAY从C#传递到COM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ATL COM服务器,其中的接口方法是

I have an ATL COM Server, where the method for the interface is

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

此函数的IDL类似

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

我试图从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);
    }

但它崩溃了。

推荐答案

我认为这里的问题是,你使用用户定义类型(UDT)的 SAFEARRAY SAFEARRAY s VARIANT BSTR IUnknown 开箱即用,但 UDT 你需要帮助编组。请参阅MSDN中关于传递UDT的安全漏洞的文章。

I think the problem here is that you're using a SAFEARRAY of user defined types (UDT), SAFEARRAYs of VARIANT, BSTR and IUnknown work out of the box but for UDTs you need to help the marshaller along. See this article in MSDN regarding Passing Safearray of UDTs.

这篇关于如何将SAFEARRAY从C#传递到COM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆