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

Strongly type UTf8 strings using SafeHandle #337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_media_add_option")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void AddOptionToMedia(IntPtr mediaInstance, IntPtr mrl);
internal delegate void AddOptionToMedia(IntPtr mediaInstance, Utf8StringHandle mrl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_media_add_option_flag")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void AddOptionFlagToMedia(IntPtr mediaInstance, IntPtr mrl, uint flag);
internal delegate void AddOptionFlagToMedia(IntPtr mediaInstance, Utf8StringHandle mrl, uint flag);
}
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 newly created media or NULL on error.</returns>
[LibVlcFunction("libvlc_media_new_location")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate IntPtr CreateNewMediaFromLocation(IntPtr instance, IntPtr mrl);
internal delegate IntPtr CreateNewMediaFromLocation(IntPtr instance, Utf8StringHandle mrl);
}
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 newly created media or NULL on error.</returns>
[LibVlcFunction("libvlc_media_new_path")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate IntPtr CreateNewMediaFromPath(IntPtr instance, IntPtr mrl);
internal delegate IntPtr CreateNewMediaFromPath(IntPtr instance, Utf8StringHandle mrl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_media_set_meta")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void SetMediaMetadata(IntPtr mediaInstance, MediaMetadatas meta, IntPtr value);
internal delegate void SetMediaMetadata(IntPtr mediaInstance, MediaMetadatas meta, Utf8StringHandle value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_audio_output_device_id")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate IntPtr GetAudioOutputDeviceName(IntPtr instance, IntPtr audioOutputName, int deviceIndex);
internal delegate IntPtr GetAudioOutputDeviceName(IntPtr instance, Utf8StringHandle audioOutputName, int deviceIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_audio_output_device_longname")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate IntPtr GetAudioOutputDeviceLongName(IntPtr instance, IntPtr audioOutputName, int deviceIndex);
internal delegate IntPtr GetAudioOutputDeviceLongName(IntPtr instance, Utf8StringHandle audioOutputName, int deviceIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_audio_output_device_set")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void SetAudioOutputDevice(IntPtr mediaPlayerInstance, IntPtr audioOutputName, IntPtr deviceName);
internal delegate void SetAudioOutputDevice(IntPtr mediaPlayerInstance, Utf8StringHandle audioOutputName, Utf8StringHandle deviceName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_audio_output_set")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void SetAudioOutput(IntPtr mediaPlayerInstance, IntPtr audioOutputName);
internal delegate void SetAudioOutput(IntPtr mediaPlayerInstance, Utf8StringHandle audioOutputName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_video_set_aspect_ratio")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void SetVideoAspectRatio(IntPtr mediaPlayerInstance, IntPtr cropGeometry);
internal delegate void SetVideoAspectRatio(IntPtr mediaPlayerInstance, Utf8StringHandle cropGeometry);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_video_set_crop_geometry")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void SetVideoCropGeometry(IntPtr mediaPlayerInstance, IntPtr cropGeometry);
internal delegate void SetVideoCropGeometry(IntPtr mediaPlayerInstance, Utf8StringHandle cropGeometry);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_video_set_deinterlace")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void SetVideoDeinterlace(IntPtr mediaPlayerInstance, IntPtr mode);
internal delegate void SetVideoDeinterlace(IntPtr mediaPlayerInstance, Utf8StringHandle mode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_video_set_logo_string")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void SetVideoLogoString(IntPtr mediaPlayerInstance, VideoLogoOptions option, IntPtr value);
internal delegate void SetVideoLogoString(IntPtr mediaPlayerInstance, VideoLogoOptions option, Utf8StringHandle value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ namespace Vlc.DotNet.Core.Interops.Signatures
/// </summary>
[LibVlcFunction("libvlc_video_set_marquee_string")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void SetVideoMarqueeString(IntPtr mediaPlayerInstance, VideoMarqueeOptions option, IntPtr value);
internal delegate void SetVideoMarqueeString(IntPtr mediaPlayerInstance, VideoMarqueeOptions option, Utf8StringHandle value);
}
4 changes: 2 additions & 2 deletions src/Vlc.DotNet.Core.Interops/Utf8InteropStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static string Utf8InteropToString(IntPtr ptr)
/// </summary>
/// <param name="source">The source string to be converted to UTF-8 so that it can be passed to libvlc.</param>
/// <returns>The safe handle</returns>
public static SafeHandle ToUtf8Interop(string source)
public static Utf8StringHandle ToUtf8StringHandle(string source)
{
if (source == null)
{
Expand All @@ -58,7 +58,7 @@ public static SafeHandle ToUtf8Interop(string source)
throw;
}

return new SafeUnmanagedMemoryHandle(buffer);
return new Utf8StringHandle(buffer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
#if !NETSTANDARD1_3
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
#endif
internal sealed class SafeUnmanagedMemoryHandle : SafeHandle
public sealed class Utf8StringHandle : SafeHandle
{
/// <summary>
/// Initializes a new instance of the <see cref="SafeUnmanagedMemoryHandle"/> class by providing the handle to be stored.
/// Initializes a new instance of the <see cref="Utf8StringHandle"/> class by providing the handle to be stored.
/// </summary>
/// <param name="preexistingHandle">The handle that is stored by this instance at initialization.</param>
internal SafeUnmanagedMemoryHandle(IntPtr preexistingHandle)
internal Utf8StringHandle(IntPtr stringHandle)
: base(IntPtr.Zero, true)
{
this.SetHandle(preexistingHandle);
this.SetHandle(stringHandle);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core.Interops
Expand All @@ -12,9 +10,9 @@ public void AddOptionFlagToMedia(VlcMediaInstance mediaInstance, string option,
if (mediaInstance == IntPtr.Zero)
throw new ArgumentException("Media instance is not initialized.");

using (var handle = Utf8InteropStringConverter.ToUtf8Interop(option))
using (var handle = Utf8InteropStringConverter.ToUtf8StringHandle(option))
{
GetInteropDelegate<AddOptionFlagToMedia>().Invoke(mediaInstance, handle.DangerousGetHandle(), flag);
GetInteropDelegate<AddOptionFlagToMedia>().Invoke(mediaInstance, handle, flag);
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/Vlc.DotNet.Core.Interops/VlcManager.AddOptionToMedia.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core.Interops
{
public sealed partial class VlcManager
public sealed partial class VlcManager
{
public void AddOptionToMedia(VlcMediaInstance mediaInstance, string option)
{
Expand All @@ -14,9 +12,9 @@ public void AddOptionToMedia(VlcMediaInstance mediaInstance, string option)
if (string.IsNullOrEmpty(option))
return;

using (var handle = Utf8InteropStringConverter.ToUtf8Interop(option))
using (var handle = Utf8InteropStringConverter.ToUtf8StringHandle(option))
{
GetInteropDelegate<AddOptionToMedia>().Invoke(mediaInstance, handle.DangerousGetHandle());
GetInteropDelegate<AddOptionToMedia>().Invoke(mediaInstance, handle);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;
using System.Text;
using Vlc.DotNet.Core.Interops.Signatures;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core.Interops
{
Expand All @@ -10,9 +8,9 @@ public VlcMediaInstance CreateNewMediaFromLocation(string mrl)
{
EnsureVlcInstance();

using (var handle = Utf8InteropStringConverter.ToUtf8Interop(mrl))
using (var handle = Utf8InteropStringConverter.ToUtf8StringHandle(mrl))
{
return VlcMediaInstance.New(this, GetInteropDelegate<CreateNewMediaFromLocation>().Invoke(myVlcInstance, handle.DangerousGetHandle()));
return VlcMediaInstance.New(this, GetInteropDelegate<CreateNewMediaFromLocation>().Invoke(myVlcInstance, handle));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;
using System.Text;
using Vlc.DotNet.Core.Interops.Signatures;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core.Interops
{
Expand All @@ -10,9 +8,9 @@ public VlcMediaInstance CreateNewMediaFromPath(string mrl)
{
EnsureVlcInstance();

using (var handle = Utf8InteropStringConverter.ToUtf8Interop(mrl))
using (var handle = Utf8InteropStringConverter.ToUtf8StringHandle(mrl))
{
return VlcMediaInstance.New(this, GetInteropDelegate<CreateNewMediaFromPath>().Invoke(myVlcInstance, handle.DangerousGetHandle()));
return VlcMediaInstance.New(this, GetInteropDelegate<CreateNewMediaFromPath>().Invoke(myVlcInstance, handle));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Vlc.DotNet.Core.Interops.Signatures;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core.Interops
{
Expand All @@ -9,10 +8,10 @@ public string GetAudioOutputDeviceLongName(string audioOutputDescriptionName, in
{
EnsureVlcInstance();

using (var audioOutputDescriptionNameInterop = Utf8InteropStringConverter.ToUtf8Interop(audioOutputDescriptionName))
using (var audioOutputDescriptionNameInterop = Utf8InteropStringConverter.ToUtf8StringHandle(audioOutputDescriptionName))
{
return Utf8InteropStringConverter.Utf8InteropToString(GetInteropDelegate<GetAudioOutputDeviceLongName>()
.Invoke(myVlcInstance, audioOutputDescriptionNameInterop.DangerousGetHandle(), deviceIndex));
.Invoke(myVlcInstance, audioOutputDescriptionNameInterop, deviceIndex));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public string GetAudioOutputDeviceName(string audioOutputDescriptionName, int de
{
EnsureVlcInstance();

using (var audioOutputInterop = Utf8InteropStringConverter.ToUtf8Interop(audioOutputDescriptionName))
using (var audioOutputInterop = Utf8InteropStringConverter.ToUtf8StringHandle(audioOutputDescriptionName))
{
return Utf8InteropStringConverter.Utf8InteropToString(GetInteropDelegate<GetAudioOutputDeviceName>().Invoke(myVlcInstance, audioOutputInterop.DangerousGetHandle(), deviceIndex));
return Utf8InteropStringConverter.Utf8InteropToString(GetInteropDelegate<GetAudioOutputDeviceName>().Invoke(myVlcInstance, audioOutputInterop, deviceIndex));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Vlc.DotNet.Core.Interops/VlcManager.SetAudioOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public void SetAudioOutput(string outputName)
{
EnsureVlcInstance();

using (var outputInterop = Utf8InteropStringConverter.ToUtf8Interop(outputName))
using (var outputInterop = Utf8InteropStringConverter.ToUtf8StringHandle(outputName))
{
GetInteropDelegate<SetAudioOutput>().Invoke(myVlcInstance, outputInterop.DangerousGetHandle());
GetInteropDelegate<SetAudioOutput>().Invoke(myVlcInstance, outputInterop);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using Vlc.DotNet.Core.Interops.Signatures;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core.Interops
{
public sealed partial class VlcManager
{
public void SetAudioOutputDevice(VlcMediaPlayerInstance mediaPlayerInstance, string audioOutputDescriptionName, string deviceName)
{
using (var audioOutputInterop = Utf8InteropStringConverter.ToUtf8Interop(audioOutputDescriptionName))
using (var deviceNameInterop = Utf8InteropStringConverter.ToUtf8Interop(audioOutputDescriptionName))
using (var audioOutputInterop = Utf8InteropStringConverter.ToUtf8StringHandle(audioOutputDescriptionName))
using (var deviceNameInterop = Utf8InteropStringConverter.ToUtf8StringHandle(audioOutputDescriptionName))
{
GetInteropDelegate<SetAudioOutputDevice>().Invoke(mediaPlayerInstance, audioOutputInterop.DangerousGetHandle(), deviceNameInterop.DangerousGetHandle());
GetInteropDelegate<SetAudioOutputDevice>().Invoke(mediaPlayerInstance, audioOutputInterop, deviceNameInterop);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/Vlc.DotNet.Core.Interops/VlcManager.SetMediaMeta.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core.Interops
Expand All @@ -11,9 +9,9 @@ public void SetMediaMeta(VlcMediaInstance mediaInstance, MediaMetadatas metadata
{
if (mediaInstance == IntPtr.Zero)
throw new ArgumentException("Media instance is not initialized.");
using (var handle = Utf8InteropStringConverter.ToUtf8Interop(value))
using (var handle = Utf8InteropStringConverter.ToUtf8StringHandle(value))
{
GetInteropDelegate<SetMediaMetadata>().Invoke(mediaInstance, metadata, handle.DangerousGetHandle());
GetInteropDelegate<SetMediaMetadata>().Invoke(mediaInstance, metadata, handle);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public void SetVideoAspectRatio(VlcMediaPlayerInstance mediaPlayerInstance, stri
if (mediaPlayerInstance == IntPtr.Zero)
throw new ArgumentException("Media player instance is not initialized.");

using (var aspectRatioInterop = Utf8InteropStringConverter.ToUtf8Interop(aspectRatio))
using (var aspectRatioInterop = Utf8InteropStringConverter.ToUtf8StringHandle(aspectRatio))
{
GetInteropDelegate<SetVideoAspectRatio>().Invoke(mediaPlayerInstance, aspectRatioInterop.DangerousGetHandle());
GetInteropDelegate<SetVideoAspectRatio>().Invoke(mediaPlayerInstance, aspectRatioInterop);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public void SetVideoCropGeometry(VlcMediaPlayerInstance mediaPlayerInstance, str
if (mediaPlayerInstance == IntPtr.Zero)
throw new ArgumentException("Media player instance is not initialized.");

using (var cropGeometryInterop = Utf8InteropStringConverter.ToUtf8Interop(cropGeometry))
using (var cropGeometryInterop = Utf8InteropStringConverter.ToUtf8StringHandle(cropGeometry))
{
GetInteropDelegate<SetVideoCropGeometry>()
.Invoke(mediaPlayerInstance, cropGeometryInterop.DangerousGetHandle());
.Invoke(mediaPlayerInstance, cropGeometryInterop);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public void SetVideoDeinterlace(VlcMediaPlayerInstance mediaPlayerInstance, stri
{
if (mediaPlayerInstance == IntPtr.Zero)
throw new ArgumentException("Media player instance is not initialized.");
using (var deinterlaceModeInterop = Utf8InteropStringConverter.ToUtf8Interop(deinterlaceMode))
using (var deinterlaceModeInterop = Utf8InteropStringConverter.ToUtf8StringHandle(deinterlaceMode))
{
GetInteropDelegate<SetVideoDeinterlace>().Invoke(mediaPlayerInstance, deinterlaceModeInterop.DangerousGetHandle());
GetInteropDelegate<SetVideoDeinterlace>().Invoke(mediaPlayerInstance, deinterlaceModeInterop);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Vlc.DotNet.Core.Interops/VlcManager.SetVideoLogo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public void SetVideoLogoFile(VlcMediaPlayerInstance mediaPlayerInstance, string
{
if (mediaPlayerInstance == IntPtr.Zero)
throw new ArgumentException("Media player instance is not initialized.");
using (var valueInterop = Utf8InteropStringConverter.ToUtf8Interop(value))
using (var valueInterop = Utf8InteropStringConverter.ToUtf8StringHandle(value))
{
GetInteropDelegate<SetVideoLogoString>().Invoke(mediaPlayerInstance, VideoLogoOptions.File, valueInterop.DangerousGetHandle());
GetInteropDelegate<SetVideoLogoString>().Invoke(mediaPlayerInstance, VideoLogoOptions.File, valueInterop);
}
}
public void SetVideoLogoX(VlcMediaPlayerInstance mediaPlayerInstance, int value)
Expand Down
4 changes: 2 additions & 2 deletions src/Vlc.DotNet.Core.Interops/VlcManager.SetVideoMarquee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public void SetVideoMarqueeText(VlcMediaPlayerInstance mediaPlayerInstance, stri
{
if (mediaPlayerInstance == IntPtr.Zero)
throw new ArgumentException("Media player instance is not initialized.");
using (var valueInterop = Utf8InteropStringConverter.ToUtf8Interop(value))
using (var valueInterop = Utf8InteropStringConverter.ToUtf8StringHandle(value))
{
GetInteropDelegate<SetVideoMarqueeString>().Invoke(mediaPlayerInstance, VideoMarqueeOptions.Text, valueInterop.DangerousGetHandle());
GetInteropDelegate<SetVideoMarqueeString>().Invoke(mediaPlayerInstance, VideoMarqueeOptions.Text, valueInterop);
}
}
public void SetVideoMarqueeColor(VlcMediaPlayerInstance mediaPlayerInstance, int value)
Expand Down