C#:将字符串编组为utf8 char*

本文关键字:utf8 char 字符串 | 更新日期: 2023-09-27 18:20:51

背景

我正在尝试编写一个基于修改后的libspotify.net的高级libspotify包装器(http://libspotifydotnet.codeplex.com/)。由于libspotify.net只是一个薄薄的(并且完全被bug…)pinvoke层,它不处理libspotify使用的utf8编码。

我的想法是将字符串转换为byte[]并适当地更改签名。

我有这个原生结构:

typedef struct sp_session_config {
  int api_version;                       
  const char *cache_location;           
  const char *settings_location;                    
  const void *application_key;           
  size_t application_key_size;           
  const char *user_agent;                
  const sp_session_callbacks *callbacks; 
  void *userdata;          
  bool compress_playlists;
  bool dont_save_metadata_for_playlists;
  bool initially_unload_playlists;
  const char *device_id;
  const char *proxy;
  const char *proxy_username;
  const char *proxy_password;
  const char *ca_certs_filename;
  const char *tracefile;
} sp_session_config;

工作版本:

    [DllImport("libspotify")]
    public static extern sp_error sp_session_create(ref sp_session_config config, out sp_session sessionPtr);
    public struct sp_session_config
    {
        public int api_version;
        public IntPtr cache_location;
        public IntPtr settings_location;
        public IntPtr application_key;
        public uint application_key_size;
        public IntPtr user_agent;
        public IntPtr callbacks;
        public IntPtr userdata;
        public bool compress_playlists;
        public bool dont_save_metadata_for_playlists;
        public bool initially_unload_playlists;
        public IntPtr device_id;
        public IntPtr proxy;
        public IntPtr proxy_username;
        public IntPtr proxy_password;
        public IntPtr ca_certs_filename;
        public IntPtr tracefile;
    }

此版本将工作交给使用库的开发人员。

我的版本:

    public struct sp_session_config_internal
    {
        public int api_version;
        public byte[] cache_location;
        public byte[] settings_location;
        public byte[] application_key;
        public uint application_key_size;
        public byte[] user_agent;
        public IntPtr callbacks;
        public IntPtr userdata;
        public bool compress_playlists;
        public bool dont_save_metadata_for_playlists;
        public bool initially_unload_playlists;
        public byte[] device_id;
        public byte[] proxy;
        public byte[] proxy_username;
        public byte[] proxy_password;
        public byte[] ca_certs_filename;
        public byte[] tracefile;
    }

    [DllImport("libspotify", EntryPoint="sp_session_create")]
    private static extern sp_error sp_session_create_internal(ref sp_session_config_internal config, out sp_session sessionPtr);
    public static sp_error sp_session_create(ref sp_session_config config, out sp_session sessionPtr)
    {
        sp_session_config_internal config_internal = new sp_session_config_internal();
        config_internal.api_version = config.api_version;
        config_internal.application_key = config.application_key;
        config_internal.application_key_size = (uint)config.application_key.Length;
        config_internal.ca_certs_filename = SH.StringToUtf8Bytes( config.ca_certs_filename);
    ...
        var err = sp_session_create_internal(ref config_internal, out session);
    ...
    }

运行时,这会在libspotify内部产生以下错误:读取位置0x000001c0时发生访问冲突我在谷歌上搜索并阅读了一些地方,有时只有第一个数组元素被复制。我试着用装饰所有阵列

        [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1)]

这导致异常"无法封送类型为sp_session_config_internal的字段'cache_location':无效的托管/非托管类型组合(数组字段必须与ByValArray或SafeArray成对出现)。"

tl;dr

IntPtr和手动编组的复杂解决方案有效,而utf8字符串的字节数组的更简单解决方案在库读取时会产生访问冲突。有没有比使用intptr手动编组更简单的方法?

C#:将字符串编组为utf8 char*

我已经解决了完全相同的问题-为.NET包装libspotify。我走了IntPtr路线,并编写了一个助手类来进行编组。我发现阵列编组简直让人抓狂,不值得把你的头发扯掉。

这是助手类:

https://github.com/openhome/ohLibSpotify/blob/master/src/ohLibSpotify/Utf8String.cs#L99

简化版:

internal class Utf8String : IDisposable
{
    IntPtr iPtr;
    public IntPtr IntPtr { get { return iPtr; } }
    public int BufferLength { get { return iBufferSize; } }
    int iBufferSize;
    public Utf8String(string aValue)
    {
        if (aValue == null)
        {
            iPtr = IntPtr.Zero;
        }
        else
        {
            byte[] bytes = Encoding.UTF8.GetBytes(aValue);
            iPtr = Marshal.AllocHGlobal(bytes.Length + 1);
            Marshal.Copy(bytes, 0, iPtr, bytes.Length);
            Marshal.WriteByte(iPtr, bytes.Length, 0);
            iBufferSize = bytes.Length + 1;
        }
    }
    public void Dispose()
    {
        if (iPtr != IntPtr.Zero)
        {
            Marshal.FreeHGlobal(iPtr);
            iPtr = IntPtr.Zero;
        }
    }
}

用法如下:

https://github.com/openhome/ohLibSpotify/blob/master/src/ohLibSpotify/SpotifySession.cs#L104

public static SpotifySession Create(SpotifySessionConfig config)
{
    IntPtr sessionPtr = IntPtr.Zero;
    IntPtr listenerToken;
    using (var cacheLocation = SpotifyMarshalling.StringToUtf8(config.CacheLocation))
    using (var settingsLocation = SpotifyMarshalling.StringToUtf8(config.SettingsLocation))
    using (var userAgent = SpotifyMarshalling.StringToUtf8(config.UserAgent))
    using (var deviceId = SpotifyMarshalling.StringToUtf8(config.DeviceId))
    using (var proxy = SpotifyMarshalling.StringToUtf8(config.Proxy))
    using (var proxyUsername = SpotifyMarshalling.StringToUtf8(config.ProxyUsername))
    using (var proxyPassword = SpotifyMarshalling.StringToUtf8(config.ProxyPassword))
    using (var traceFile = SpotifyMarshalling.StringToUtf8(config.TraceFile))
    {
        IntPtr appKeyPtr = IntPtr.Zero;
        listenerToken = ListenerTable.PutUniqueObject(config.Listener, config.UserData);
        try
        {
            NativeCallbackAllocation.AddRef();
            byte[] appkey = config.ApplicationKey;
            appKeyPtr = Marshal.AllocHGlobal(appkey.Length);
            Marshal.Copy(config.ApplicationKey, 0, appKeyPtr, appkey.Length);
            sp_session_config nativeConfig = new sp_session_config {
                api_version = config.ApiVersion,
                cache_location = cacheLocation.IntPtr,
                settings_location = settingsLocation.IntPtr,
                application_key = appKeyPtr,
                application_key_size = (UIntPtr)appkey.Length,
                user_agent = userAgent.IntPtr,
                callbacks = SessionDelegates.CallbacksPtr,
                userdata = listenerToken,
                compress_playlists = config.CompressPlaylists,
                dont_save_metadata_for_playlists = config.DontSaveMetadataForPlaylists,
                initially_unload_playlists = config.InitiallyUnloadPlaylists,
                device_id = deviceId.IntPtr,
                proxy = proxy.IntPtr,
                proxy_username = proxyUsername.IntPtr,
                proxy_password = proxyPassword.IntPtr,
                tracefile = traceFile.IntPtr,
            };
            // Note: sp_session_create will invoke a callback, so it's important that
            // we have already done ListenerTable.PutUniqueObject before this point.
            var error = NativeMethods.sp_session_create(ref nativeConfig, ref sessionPtr);
            SpotifyMarshalling.CheckError(error);
        }
        catch
        {
            ListenerTable.ReleaseObject(listenerToken);
            NativeCallbackAllocation.ReleaseRef();
            throw;
        }
        finally
        {
            if (appKeyPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(appKeyPtr);
            }
        }
    }
    SpotifySession session = SessionTable.GetUniqueObject(sessionPtr);
    session.Listener = config.Listener;
    session.UserData = config.UserData;
    session.ListenerToken = listenerToken;
    return session;
}

因为手工编写这些东西很快就会变得乏味,所以绝大多数包装器和DllImport声明都是从api.h头文件自动生成的。(因此,你不会在github repo中找到它们,你需要下载项目并构建它来查看所有生成的代码。)

这一切都是自由许可的(2段BSD),所以可以随意使用它或借用它的一些部分。