Specifically, if I make a struct that has a single field, that essentially acts as a wrapper around that value, is it safe to pass this struct to a P/Invoke method expecting the underlying type?
I'm working with a native library whose API involves a lot of pointer-to-struct types, and I'd like to use something a bit more typesafe than IntPtr
to keep them all straight, by wrapping IntPtr
in a generic struct. Would that work? (And has it already been done?)
struct
is just a blob in-memory, nothing more. It doesn't have any additional overhead other then the members of thestruct
itself. You can even specify the way you want to layout the struct in memory usingStructLayoutAttribute
. – TranscendenceSomeFunction(IntPtr foo)
toSomeFunction(MyStruct foo)
? – Gordenref MyStruct foo
would be ideal, except that I get theMyStruct
from the external library, from a function that returns aMyStruct*
. – Pelagia