I have an ATL COM Server, where the method for the interface is
CVivsBasic::UpdateSwitchPlan(BSTR plan_name, SAFEARRAY* plan)
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) ;
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);
}
But it crash. What is the correct way to pass a SAFEARRAY from C# to 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. See this article in MSDN regarding Passing Safearray of UDTs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With