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?