Marshal filename to UTF-8
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)
-
reporter -
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.
-
reporter Thank you for check.
Assimp library seems to use UTF-8 as an internal codepoint, so that
byte[] filename
(orAiString
) 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. -
.Net7 did actually introduce a new way of Interop that seems to handle UTF8 Marshaling for you.
Instead of [DllImport("mydll.dll")] you do [LibraryImport("mydll.dll", StringMarshalling = StringMarshalling.Utf8)] fixed all my troubles with umlauts in filenames i had with a different c++ lib.
Documentation: https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation.
.NetConf2022 Talk about it : https://youtu.be/ucNcRtnifXY -
repo owner Cool, looks like it can also replace a lot of the plumbing for how I marshal the assimp types too.
-
reporter Thanks, it looks really nice. I still not be ready to .NET7, but it will be resolve Marshaling.
-
@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.
- Log in to comment
I create an example project on github.
Currently I needs
AssimpContext.ImportFile
so that AssimpContext_ImportFile_MarshalAs_LPUTF8Str.cs includes just a cut out code.I guess a better all-embracing way to convert
UTF-8
string inAssimp
toUnicode
string in.NET
. I don’t know how it is.