[Mono-list] Reference/ Pointer passing
Jonathan Pryor
jonpryor at vt.edu
Fri Oct 21 07:23:44 EDT 2005
On Fri, 2005-10-21 at 01:38 -0700, itsme notyou wrote:
> I am not clear about how the CLR will act if we pass a reference OR
> pointer to a unmanaged code via Pinvoke. ( Both are treated alike with
> the only diff being we need to encapsulate the code using '&' with
> UNSAFE and pin the location ????? )
>
> I read the useful article by Jon Pryor
> (http://www.mono-project.com/index.php?title=Interop_with_Native_Libraries&redirect=no). It states the 4 steps for marshaling...what happens if you pass a NON_BLITTABLE STRUCTURE by reference (without pinning it) or pass a pointer to this structure.
Structures are on the stack, and thus don't need to be pinned. If the
structure isn't blittable, the runtime will marshal the structure to
unmanaged memory (creating a copy), and pass a pointer to the marshaled
copy to the unmanaged function. If [In, Out] was applied to the
P/Invoke parameter, the copy will be unmarshaled into the original
structure.
The same will happen for classes with a [StructLayout] attribute.
> Will this be
>
> Content of pointer copied to UM memory---- > marshaling done --- >
> ( Unmanaged code makes operates on it / changes ) ---> result
> marshaled BACK to managed types ---> result copied to managed memory
Yes.
> And do the above steps take place for "class pass by reference" ???
Yes.
There is one caveat: if Mono cannot marshal the class/structure, for
whatever reason, the process will be aborted. Goodbye, cruel world...
See also:
http://lists.ximian.com/pipermail/mono-list/2005-October/029117.html
- Jon
More information about the Mono-list
mailing list