Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Fixed #74 by adding support for UTF-8 in options
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyVignelles committed Sep 18, 2017
1 parent 3fd6915 commit eaa8f54
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// <returns>Return the libvlc instance or NULL in case of error.</returns>
[LibVlcFunction("libvlc_new")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate IntPtr CreateNewInstance(int argc, string[] argv);
internal delegate IntPtr CreateNewInstance(int argc, IntPtr[] argv);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AddOptionToMedia>().Invoke(mediaInstance, handle.AddrOfPinnedObject());
handle.Free();
}
Expand Down
33 changes: 27 additions & 6 deletions src/Vlc.DotNet.Core.Interops/VlcManager.CreateNewInstance.cs
Original file line number Diff line number Diff line change
@@ -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<CreateNewInstance>().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<CreateNewInstance>().Invoke(utf8Args.Length, utf8Args));
}
finally
{
foreach (var arg in utf8Args)
{
Marshal.FreeHGlobal(arg);
}
}
}
}
}

0 comments on commit eaa8f54

Please sign in to comment.