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

lil*_*ind 5 c# com

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

CVivsBasic::UpdateSwitchPlan(BSTR plan_name, SAFEARRAY* plan)
Run Code Online (Sandbox Code Playgroud)

这个函数的 IDL 看起来像

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

我尝试从 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);
    }
Run Code Online (Sandbox Code Playgroud)

但它崩溃了。将 SAFEARRAY 从 C# 传递到 COM 的正确方法是什么?

Mot*_*tti 2

我认为这里的问题是您正在使用SAFEARRAY用户定义类型(UDT)、SAFEARRAYs ofVARIANT和开箱即用的功能,但对于BSTRs您需要帮助编组器。请参阅 MSDN 中有关传递 UDT Safearray 的文章。IUnknownUDT