Marshal filename to UTF-8

Issue #86 new
Ema Ema created an issue

AssimpContext.ImportFile can not load files or materials which includes a multi-byte string.

Please specify UnmanagedType.LPUTF8Str instead of UnmanagedType.LP8Str for I/O functions to DllImport (UnmanagedType Enum).

It works fine by way of trial, but I'm not sure that is correct way to path UTF-8 string to DLL method (Roslyn reports CA2101).

    [DllImport("assimp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "aiImportFileExWithProperties")]
    private static extern IntPtr AiImportFileExWithProperties([In, MarshalAs(UnmanagedType.LPUTF8Str)] string file, uint flag, IntPtr fileIO, IntPtr propStore);

I will create a simple project for test in this weekend.

regards.

Comments (7)

  1. Nicholas Woodfield repo owner

    That unmanaged type isn’t supported in the .net standard the library targets, a fix would have to marshal it manually before passing it to the native layer. I see there’s been some advancement on supporting UTF8 in the native library since I last looked (years lol), so there probably will be other code points (e.g. string names in returned data) where there may be failures too.

  2. Ema Ema reporter

    Thank you for check.

    Assimp library seems to use UTF-8 as an internal codepoint, so that byte[] filename (or AiString) might be an alternative API way (library user has a responsibility to ensure UTF-8 encoding from outside).

    I know that is not easy to use and is not desirable changes. `.NET 7` still be not settled on how to handle UTF-8, so that I cannot believe the way is suitable one (although it seems likely that it will be a raw byte sequence).

    Fortunately I can use patched own class to use currently.
    I will do so for the time being.

  3. Nicholas Woodfield repo owner

    Cool, looks like it can also replace a lot of the plumbing for how I marshal the assimp types too.

  4. Ema Ema reporter

    Thanks, it looks really nice. I still not be ready to .NET7, but it will be resolve Marshaling.

  5. Jan Selchow

    @Nicholas Woodfield Would you consider using it? I know it would probably mean maintaining code for two different target runtimes but not having to worry about the UTF-8 marshalling out of the box would be great.

  6. Log in to comment