AgoraIO-Community / Agora-Unity-RTM-SDK

A Unity3D sample app to show Login /Logout, Log in to/out of channel, Get Channel Member Count, Send/Receive Channel Message, Send Peer Message, Query Member, and token Authentication
24 stars 15 forks source link

带有汉字的文件路径导致createImageMessageByUploading的回调失败 #17

Open ahjszll opened 3 years ago

ahjszll commented 3 years ago

路径没有经过utf8编码,windows平台下无法选择带有汉字的文件路径。

zhangtao1104 commented 3 years ago

你这边 api 参数是怎么传递的? 我这边调用是正常的。 能提供下你的调用示例吗?

icywind commented 3 years ago

@ahjszll 这个问题解决了吗?

ahjszll commented 3 years ago

你是否也碰到了? @icywind

` [DllImport(MyLibName, CharSet = CharSet.Ansi)] protected static extern int createImageMessageByUploading(IntPtr rtmServiceInstance, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UTF8Marshaler))] string filePath, Int64 requestId);

public class UTF8Marshaler : ICustomMarshaler { private static UTF8Marshaler instance = new UTF8Marshaler(); public static ICustomMarshaler GetInstance(string cookie) { return instance; } public void CleanUpManagedData(object ManagedObj) {

    }
    public void CleanUpNativeData(IntPtr pNativeData)
    {
        Marshal.FreeHGlobal(pNativeData);
    }
    public int GetNativeDataSize()
    {
        return -1;
    }
    public IntPtr MarshalManagedToNative(object ManagedObj)
    {
        if (ManagedObj == null)
            return IntPtr.Zero;
        if (ManagedObj is string)
        {
            byte[] utf8Bytes = Encoding.UTF8.GetBytes(ManagedObj as string);
            IntPtr ptr = Marshal.AllocHGlobal(utf8Bytes.Length + 1);
            Marshal.Copy(utf8Bytes, 0, ptr, utf8Bytes.Length);
            Marshal.WriteByte(ptr, utf8Bytes.Length, 0);
            return ptr;
        }
        else
            throw new InvalidOperationException();
    }
    public object MarshalNativeToManaged(IntPtr pNativeData)
    {
        if (pNativeData == IntPtr.Zero)
            return null;
        List<byte> bytes = new List<byte>();
        for (int offset = 0; ; offset++)
        {
            byte b = Marshal.ReadByte(pNativeData, offset);
            if (b == 0)
                break;
            else
                bytes.Add(b);
        }
        return Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count);
    }
}`