托管结构和非托管结构的大小不同
我正在通过 P/Invoke 使用非托管库,它使用三个结构(尽管它们都具有相同的基本布局,所以我只发布一个):
struct Agraph_t {
int tag:4;
int kind:4;
int handle:24;
char **attr;
char *didset;
char *name;
Agdata_t *univ;
Dict_t *nodes, *inedges, *outedges;
Agraph_t *root;
Agnode_t *meta_node;
Agproto_t *proto;
Agraphinfo_t u;
};
由于我的包装器使用这些对象的方式,我必须参考到 Agraph_t
内部的结构作为 IntPtr
。我添加了一些属性,使访问位字段的值变得更加容易。
public struct Agraph_t {
public uint tag_kind_handle;
public IntPtr attr;
public string didset;
public string name;
public IntPtr univ;
public IntPtr nodes, inedges, outedges;
public IntPtr root;
public IntPtr meta_node;
public IntPtr proto;
public IntPtr u;
public uint Tag {
get { return (tag_kind_handle & 15u); }
}
public uint Kind {
get { return (tag_kind_handle & 240u) / 16; }
}
public uint Handle {
get { return (tag_kind_handle & 4294967040u) / 256; }
}
}
在执行任何操作之前,我必须通过为非托管库提供三个结构中每个结构的大小来初始化它。
aginitlib(Marshal.SizeOf(typeof(Agraph_t)), ..., ...);
这样做时我没有收到错误,并且可以很好地使用该库。但是,库的一部分使用非托管结构的大小自行调用 aginitlib (我无法控制这一点)。此时,该库警告我它已使用两种不同的大小进行初始化,这使其不稳定(在某些操作后抛出 AccessViolationException )。
添加的属性是否被考虑到结构的大小中并使其比非托管版本更大?我会删除它们并看看会发生什么,但我的代码严重依赖它们,这使得这变得很困难。
我是否需要将 StructLayoutAttribute
与 Size
属性一起使用?唯一让我困惑的是 IntPtr。该库严格是 32 位的,所以我可以继续安全地假设这些字段始终是 32 位吗?
I'm working with an unmanaged library through P/Invoke and it uses three structs (although they all have the same basic layout, so I'll only post one):
struct Agraph_t {
int tag:4;
int kind:4;
int handle:24;
char **attr;
char *didset;
char *name;
Agdata_t *univ;
Dict_t *nodes, *inedges, *outedges;
Agraph_t *root;
Agnode_t *meta_node;
Agproto_t *proto;
Agraphinfo_t u;
};
Because of the way my wrapper uses these objects, I have to refer to structs inside Agraph_t
as IntPtr
s. I added properties that make accessing the values of the bit fields easier.
public struct Agraph_t {
public uint tag_kind_handle;
public IntPtr attr;
public string didset;
public string name;
public IntPtr univ;
public IntPtr nodes, inedges, outedges;
public IntPtr root;
public IntPtr meta_node;
public IntPtr proto;
public IntPtr u;
public uint Tag {
get { return (tag_kind_handle & 15u); }
}
public uint Kind {
get { return (tag_kind_handle & 240u) / 16; }
}
public uint Handle {
get { return (tag_kind_handle & 4294967040u) / 256; }
}
}
Before doing anything, I have to initialize the unmanaged library by giving it the size of each of the three structs.
aginitlib(Marshal.SizeOf(typeof(Agraph_t)), ..., ...);
I don't get an error when doing that and I can use the library just fine. However, part of the library makes its own call to aginitlib (I have no control over this) using the size of the unmanaged structs. At that point, the library warns me that it has been initialized with two different sizes, which is making it unstable (throwing AccessViolationException
s after certain operations).
Are the added properties being factored into the size of the struct and making it larger than the unmanaged version? I would remove them and see what happens, but my code depends heavily on them, which makes that difficult.
Do I need to use StructLayoutAttribute
with the Size
property? The only thing that confuses me about that is the IntPtr
s. The library is strictly 32-bit, so can I go ahead and safely assume that those fields will be 32 bits all the time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
区别在于
u
的声明。非托管声明具有以下内容:这意味着 Agraphinfo_t 是在 Agraph_t 结构中内联分配的。例如,如果 Agraphinfo_t 的大小为 16 个字节,那么它将向 sizeof(Agraph_t) 贡献 16 个字节。
但是,在托管声明中,您像这样声明 u:
这意味着在 Agraph_t 结构中分配了一个指针。在 32 位系统上,这将为 sizeof(Agraph_t) 贡献 4 个字节。因此,为 Agraph_t 计算的两个大小不同步。
要解决此问题,请声明一个与 Agraphinfo_t 等效的托管对象,并在 Agraph_t 中创建它的实例:
The difference is because of the declaration for
u
. The unmanaged declaration has this:This means that an Agraphinfo_t is allocated inline in the Agraph_t struct. If Agraphinfo_t is, say, 16 bytes in size, then it contributes 16 bytes to sizeof(Agraph_t).
However, in your managed declaration, you declare u like this:
This means that a pointer is allocated in the Agraph_t struct. On a 32-bit system, this will contribute 4 bytes to sizeof(Agraph_t). Hence the two sizes computed for Agraph_t are out of sync.
To fix the problem, declare a managed equivalent to Agraphinfo_t and create an instance of that in Agraph_t:
IntPtr 在 32 位代码中为 4 字节宽,在 64 位模式中为 8 字节长。
您可以使用 IntPtr.Size 属性来确定大小,该属性报告当前运行时的值的大小。
至于您的其他问题,您确实应该使用
StructLayoutAttribute
来确保托管和非托管结构以相同的方式在内存中布局,并具有相同的填充和字段大小。您还需要从结构中删除属性,因为它们会影响内存中结构的大小。
IntPtr is 4-bytes wide in 32bit code and will be 8-bytes long in 64bit mode.
You can determine the size by using the IntPtr.Size property, which reports the size of the value for the current runtime.
As for your other question, you should really use
StructLayoutAttribute
to ensure that the managed and unmanaged structures are laid out in memory in the same way,with the same padding and field sizes.You will also need to remove the properties from the struct, since they will affect the size of the structure in memory.