safearrays 可以接受并编组用户定义的类型吗?
我想将相当复杂的类型从托管世界转移到本机世界,反之亦然。
目前,这是通过创建多维安全数组来完成的,其优点是可以为您完成编组,但意味着我们最终会得到相当复杂的锯齿状数组来帮助我们理解。
我尝试将一些结构放入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论