[Mono-dev] using unsafe structs as opaque pointers...

Rodrigo Kumpera kumpera at gmail.com
Thu Aug 23 17:41:44 UTC 2012


This has been fixed on mono master, on what will become 2.12.

On Thu, Aug 16, 2012 at 1:35 PM, jeske <davidj at gmail.com> wrote:

> I have a C# wrapper for a native DLL which exists on both windows and UNIX.
> However, it doesn't work on Mono because of a behavior difference between
> Mono and MS.NET.
>
> Specifically, I use "unsafe struct pointers" to manage pointers to opaque
> data (rather than IntPtr). I do this to get typechecking, because there are
> a bunch of types of pointers, and using IntPtr is dangerously
> non-type-checked.  (I honestly don't undertand why IntPtr is allowed,
> rather
> than always requiring the use of typed unsafe-struct-pointers.)
>
>
> http://stackoverflow.com/questions/4503795/is-it-valid-to-use-unsafe-struct-as-an-opaque-type-instead-of-intptr-in-net-p
>
> My code looks something like:
>
> // opaque types
> public unsafe struct HDF {};
>
> // NEOERR* hdf_set_value (HDF *hdf, char *name, char *value)
> [DllImport("libneo")]
> private static unsafe extern NEOERR* hdf_set_value(HDF *hdf,
>      [MarshalAs(UnmanagedType.LPStr)]
>       string name,
>      [MarshalAs(UnmanagedType.LPStr)]
>       string value);
>
> This works fine in MS.NET, but Mono complains with the following error:
>
> Unhandled Exception:
> System.Runtime.InteropServices.MarshalDirectiveException: Can not marshal
> 'parameter #1': Pointers can not reference marshaled structures. Use byref
> instead.
>   at (wrapper managed-to-native) Clearsilver.Hdf:hdf_set_value
> (Clearsilver.HDF*,string,string)
>   at Clearsilver.Hdf.setValue (System.String name, System.String value)
> [0x00000]
>   at CSTest.Main (System.String[] argv) [0x00000]
>
> My questions:
>
> (1) Why is Mono complaining about this? In the CLR, an unsafe struct
> pointer
> is not a marshalled structure, it's a value type.
>
> (2) Why would it suggest using ByRef? ByRef is for managed pointers to
> managed data. This is a value-type pointer value.  AFAIK, ByRef is not
> capable of doing anything here, because the pointer is just a "typed"
> IntPtr
> value type.
>
>
>
>
>
>
> --
> View this message in context:
> http://mono.1490590.n4.nabble.com/using-unsafe-structs-as-opaque-pointers-tp4656281.html
> Sent from the Mono - Dev mailing list archive at Nabble.com.
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20120823/3ce74f59/attachment.html>


More information about the Mono-devel-list mailing list