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 intptrs, 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

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -