From eaa8f5460d4cc4ef44fe0036e59b9680f0a57dea Mon Sep 17 00:00:00 2001 From: Jeremy Vignelles Date: Mon, 18 Sep 2017 14:47:25 +0200 Subject: [PATCH] Fixed #74 by adding support for UTF-8 in options --- .../Signatures/libvlc.h/libvlc_new.cs | 2 +- .../VlcManager.AddOptionToMedia.cs | 2 +- .../VlcManager.CreateNewInstance.cs | 33 +++++++++++++++---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Vlc.DotNet.Core.Interops/Signatures/libvlc.h/libvlc_new.cs b/src/Vlc.DotNet.Core.Interops/Signatures/libvlc.h/libvlc_new.cs index 8767bcc..d626376 100644 --- a/src/Vlc.DotNet.Core.Interops/Signatures/libvlc.h/libvlc_new.cs +++ b/src/Vlc.DotNet.Core.Interops/Signatures/libvlc.h/libvlc_new.cs @@ -9,5 +9,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures /// Return the libvlc instance or NULL in case of error. [LibVlcFunction("libvlc_new")] [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - internal delegate IntPtr CreateNewInstance(int argc, string[] argv); + internal delegate IntPtr CreateNewInstance(int argc, IntPtr[] argv); } diff --git a/src/Vlc.DotNet.Core.Interops/VlcManager.AddOptionToMedia.cs b/src/Vlc.DotNet.Core.Interops/VlcManager.AddOptionToMedia.cs index 39054d8..cdacd5b 100644 --- a/src/Vlc.DotNet.Core.Interops/VlcManager.AddOptionToMedia.cs +++ b/src/Vlc.DotNet.Core.Interops/VlcManager.AddOptionToMedia.cs @@ -13,7 +13,7 @@ public void AddOptionToMedia(VlcMediaInstance mediaInstance, string option) throw new ArgumentException("Media instance is not initialized."); if (string.IsNullOrEmpty(option)) return; - var handle = GCHandle.Alloc(Encoding.ASCII.GetBytes(option), GCHandleType.Pinned); + var handle = GCHandle.Alloc(Encoding.UTF8.GetBytes(option), GCHandleType.Pinned); GetInteropDelegate().Invoke(mediaInstance, handle.AddrOfPinnedObject()); handle.Free(); } diff --git a/src/Vlc.DotNet.Core.Interops/VlcManager.CreateNewInstance.cs b/src/Vlc.DotNet.Core.Interops/VlcManager.CreateNewInstance.cs index c5b9d08..762b5f1 100644 --- a/src/Vlc.DotNet.Core.Interops/VlcManager.CreateNewInstance.cs +++ b/src/Vlc.DotNet.Core.Interops/VlcManager.CreateNewInstance.cs @@ -1,14 +1,35 @@ -using Vlc.DotNet.Core.Interops.Signatures; - -namespace Vlc.DotNet.Core.Interops +namespace Vlc.DotNet.Core.Interops { + using System; + using System.Runtime.InteropServices; + using Vlc.DotNet.Core.Interops.Signatures; + using System.Text; + public sealed partial class VlcManager { public void CreateNewInstance(string[] args) { - if (args == null) - args = new string[0]; - myVlcInstance = new VlcInstance(this, GetInteropDelegate().Invoke(args.Length, args)); + IntPtr[] utf8Args = new IntPtr[args?.Length ?? 0]; + try + { + for (var i = 0; i < utf8Args.Length; i++) + { + byte[] bytes = Encoding.UTF8.GetBytes(args[i]); + var buffer = Marshal.AllocHGlobal(bytes.Length + 1); + Marshal.Copy(bytes, 0, buffer, bytes.Length); + Marshal.WriteByte(buffer, bytes.Length, 0); + utf8Args[i] = buffer; + } + + myVlcInstance = new VlcInstance(this, GetInteropDelegate().Invoke(utf8Args.Length, utf8Args)); + } + finally + { + foreach (var arg in utf8Args) + { + Marshal.FreeHGlobal(arg); + } + } } } }