Open ahjszll opened 3 years ago
你这边 api 参数是怎么传递的? 我这边调用是正常的。 能提供下你的调用示例吗?
@ahjszll 这个问题解决了吗?
你是否也碰到了? @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);
}
}`
路径没有经过utf8编码,windows平台下无法选择带有汉字的文件路径。