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 IntPtrs. 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 AccessViolationExceptions 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 IntPtrs. The library is strictly 32-bit, so can I go ahead and safely assume that those fields will be 32 bits all the time?
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: