[Mono-list] using 'unsafe struct *' instead of IntPtr with PInvoke..
Jonathan Pryor
jonpryor at vt.edu
Thu Dec 23 12:45:35 EST 2010
On Dec 22, 2010, at 6:35 PM, jeske wrote:
> Is there anything wrong with this pattern?
That depends on how you define "wrong." :-)
In theory, this is safe (safer, really, as you get more type checking).
In practice, it's more iffy for "social" reasons: unsafe code requires 'csc /unsafe', and some places don't like the '/unsafe' compiler flag. The /unsafe' flag also marks the assembly as untrusted, so in certain CAS contexts you won't be able to use your assembly while you would be able to use the equivalent IntPtr code.
> At a deeper level, I wonder why Platform Invoke doesn't advocate using
> something like this more often, because it's alot safer than storing and
> passing everything around as IntPtr.
Then there's the Common Language Subset (CLS) viewpoint: unsafe code is NOT part of the CLS, while IntPtr/etc. IS, so IntPtr is more "portable" across languages targeting .NET, and thus more "general." Advocating IntPtr allows Microsoft to keep their documentation shorter and just write about the more general approach.
- Jon
More information about the Mono-list
mailing list