如何从C#中的SAFEARRAY传递给COM? [英] How to pass a SAFEARRAY from C# to 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), SAFEARRAY
s of VARIANT
, BSTR
and IUnknown
work out of the box but for UDT
s you need to help the marshaller along. See this article in MSDN regarding Passing Safearray of UDTs.
这篇关于如何从C#中的SAFEARRAY传递给COM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!