marshalling - Is this C# struct guaranteed to have the desired size? -
i create type safe pointer structure wrapping intptr
:
struct pointer<t> { private intptr ptr; // methods marshalling , t }
but want able marshal pointer<t>
instances if intptr
s, need have same size , layout. guaranteed?
if not, if add
[structlayout(layoutkind.sequential, pack = 1)]
at top?
basically, @ end should able marshal c struct
struct foo { int *data; };
using c# struct:
struct foo { public pointer<int> data; }
it fine as-is, no need help.
a struct type in c# automatically gets [structlayout] attribute. default sequential packing of 8. same kind of packing used default in unmanaged code. doesn't matter anyway when have 1 field in struct.
just make sure don't add fields , don't use automatic properties. can double-check marshal.sizeof(), should 4 in 32-bit mode , 8 in 64-bit mode. or in other words, equal intptr.size
Comments
Post a Comment