导入到 C# 时有什么方法可以修复 COM 结构成员的顺序吗?
我在C++项目的.idl文件中定义了一个结构体,该结构体包含一个VARIANT成员。
[uuid(C42A456C-C139-4339-A023-F9458C8A7386)]
struct TEST_STRUCT
{
int Type;
VARIANT DateTime;
float Result;
};
界面是:
[id(1), helpstring("Test1")] HRESULT Test1([in] int nID, [out, retval] SAFEARRAY(struct TEST_STRUCT)* ppVal);
然后我通过“添加引用”将该结构体导入到 C# 项目中,但成员顺序发生了更改。看起来像这样:
namespace ASLib
{
[Guid("C42A456C-C139-4339-A023-F9458C8A7386")]
public struct TEST_STRUCT
{
public object DateTime;
public int Type;
public float Result;
}
}
DateTime 成员的顺序在 C# 中更改为第一个,当 C# 调用该接口时,它导致 Interop.COMException“错误的变量类型”。
那么有什么办法吗?修复 COM idl 文件中结构成员的顺序?多谢。
I defined a struct in .idl file of C++ project, and the struct contained one VARIANT member.
[uuid(C42A456C-C139-4339-A023-F9458C8A7386)]
struct TEST_STRUCT
{
int Type;
VARIANT DateTime;
float Result;
};
The interface is:
[id(1), helpstring("Test1")] HRESULT Test1([in] int nID, [out, retval] SAFEARRAY(struct TEST_STRUCT)* ppVal);
Then I imported this struct into C# project via "Add Reference", but the member order was changed. It looks like this:
namespace ASLib
{
[Guid("C42A456C-C139-4339-A023-F9458C8A7386")]
public struct TEST_STRUCT
{
public object DateTime;
public int Type;
public float Result;
}
}
The order of DateTime member was changed to first in C#, it caused an Interop.COMException "Bad variable type" when C# calls that interface.
So is there any way to fix the order of struct members in COM idl file? Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过 结构布局。
将
FieldOffset
属性添加到生成的字段。它可能会是这样的:
You can fix the struct layout by hand via StructLayout.
Add the
FieldOffset
attribute to the generated fields.It'll probably be something like: