如何从C#中的SAFEARRAY传递给COM? [英] How to pass a SAFEARRAY from C# to COM?

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

问题描述

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

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

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

和IDL这个功能看起来像

And the IDL for this function looks like

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



我想从C#调用它是这样的:

I tried to call it from C# like this:

        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);
    }



但它崩溃。什么是从C#一个SAFEARRAY传递给COM?

But it crash. What is the correct way to pass a SAFEARRAY from C# to COM?

推荐答案

我觉得这里的问题是,你正在使用一个 SAFEARRAY 用户定义类型(UDT), SAFEARRAY VARIANT <的S / code>, BSTR 的IUnknown 开箱,但对 UDT 就是你需要帮忙一起编组。见关于传递的UDT 的SAFEARRAY本文MSDN。

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.

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

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