[Mono-list] Need help with DllImport (P/Invoke) and UCS-4 unicode
Christian Heimes
lists at cheimes.de
Mon Jul 16 11:06:54 EDT 2007
Jonathan Pryor wrote:
[great stuff]
Thank you very much Jonathan! You and the other Jonathan are extremely
helpful. On the other hands the docs from MSDN aren't very helpful at
all. :(
Your code works like a charm and I got most unit tests running. I'm
still having a small problem. How can I apply a custom marshaler to the
return value of a pinvoke? I like to replace some code with the custom
marshaler.
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "PyUnicodeUCS4_AsUnicode",
ExactSpelling = true)]
internal unsafe static extern char*
PyUnicode_AsUnicode(IntPtr ob);
internal unsafe static string GetManagedString(IntPtr op) {
IntPtr type = PyObject_TYPE(op);
...
if (type == Runtime.PyUnicodeType) {
char *p = Runtime.PyUnicode_AsUnicode(op);
int size = Runtime.PyUnicode_GetSize(op);
return new String(p, 0, size);
}
return null;
}
> This uses the Mono.Unix.UnixMarshal class within Mono.Posix.dll.
>
> For a custom marshaler, your DllImport would be:
>
> [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl)]
> internal unsafe static extern IntPtr
> PyUnicode_FromUnicode(
> [MarshalAs (UnmanagedType.CustomMarshaler,
> MarshalTypeRef=typeof(Utf32Marshaler)]
> string path, int size);
Note: You forgot a closing bracket. ;)
Christian
More information about the Mono-list
mailing list