safearrays 可以接受并编组用户定义的类型吗?

发布于 2024-08-23 23:15:56 字数 1648 浏览 4 评论 0原文

我想将相当复杂的类型从托管世界转移到本机世界,反之亦然。

目前,这是通过创建多维安全数组来完成的,其优点是可以为您完成编组,但意味着我们最终会得到相当复杂的锯齿状数组来帮助我们理解。

我尝试将一些结构放入 SAFEARRAY 中,但遇到了与 this 未回答的问题。 msdn 似乎暗示这是可能的,但我几乎没有成功。如果可能的话,在以下情况下什么是有效的 VT_ 类型?

编辑:谢谢!所以这必须是 VT_RECORD。

struct Change
{
    PSTR key;
    PSTR val;
};

struct NodeChages
{
    int nodeId;
    SAFEARRY* changes; //CComSafeArray<Change>
};


STDAPI func(/*CComSafeArray<NodeChanges>*/ SAFEARRAY * f);

在管理方面我有。

public struct Change
{
    [MarshalAsAttribute(UnmanagedType.LPStr)]
    public string key;
    [MarshalAsAttribute(UnmanagedType.LPStr)]
    public string value;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct NodeChanges
{
    public int nodeId;
     [MarshalAsAttribute(UnmanagedType.SafeArray, SafeArrayUserDefinedSubType = typeof(CurveChange))]
    public Change[] changes;
}

[DllImportAttribute("My.dll", EntryPoint = "Func", PreserveSig = true, CallingConvention=CallingConvention.StdCall)]
   public static extern void Func(
   [MarshalAsAttribute(UnmanagedType.SafeArray, SafeArrayUserDefinedSubType = typeof(NodeChange))]
   NodeChange[] changes
   );

但这(如另一篇文章中所示)给了我:

System.ArgumentException : The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

I would like to move fairly complex types from the managed to native world and visa versa.

Currently this is being done by creating multidimensional safearrays, which has the advantage that marshalling is done for you, but means we end up with rather complex jagged arrays to get our heads around.

I have tried to put some structs into SAFEARRAYs but came across issues very similar to this unanswered question. msdn seems to imply that it is possible, but I am having little success. If this is possible, what are valid VT_ types in the following situation?

edit: Thanks! So this has to be a VT_RECORD.

struct Change
{
    PSTR key;
    PSTR val;
};

struct NodeChages
{
    int nodeId;
    SAFEARRY* changes; //CComSafeArray<Change>
};


STDAPI func(/*CComSafeArray<NodeChanges>*/ SAFEARRAY * f);

On the managed side I have.

public struct Change
{
    [MarshalAsAttribute(UnmanagedType.LPStr)]
    public string key;
    [MarshalAsAttribute(UnmanagedType.LPStr)]
    public string value;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct NodeChanges
{
    public int nodeId;
     [MarshalAsAttribute(UnmanagedType.SafeArray, SafeArrayUserDefinedSubType = typeof(CurveChange))]
    public Change[] changes;
}

[DllImportAttribute("My.dll", EntryPoint = "Func", PreserveSig = true, CallingConvention=CallingConvention.StdCall)]
   public static extern void Func(
   [MarshalAsAttribute(UnmanagedType.SafeArray, SafeArrayUserDefinedSubType = typeof(NodeChange))]
   NodeChange[] changes
   );

But this (as in the other post) gives me:

System.ArgumentException : The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文