From a4c638cf360e3bf03750fb0fd2b59f3183b9115d Mon Sep 17 00:00:00 2001 From: dahall Date: Sun, 16 Jan 2022 17:24:00 -0700 Subject: [PATCH] Removed SafeMoveableHGlobalHandle and replace with Kernel32 instance --- Windows.Shell.Common/NativeClipboard.cs | 114 +------------------------------- 1 file changed, 1 insertion(+), 113 deletions(-) diff --git a/Windows.Shell.Common/NativeClipboard.cs b/Windows.Shell.Common/NativeClipboard.cs index ab205440..46861ba3 100644 --- a/Windows.Shell.Common/NativeClipboard.cs +++ b/Windows.Shell.Common/NativeClipboard.cs @@ -13,6 +13,7 @@ using System.Threading; using Vanara.Extensions; using Vanara.InteropServices; using Vanara.PInvoke; +using static Vanara.PInvoke.Kernel32; using static Vanara.PInvoke.Ole32; using static Vanara.PInvoke.Shell32; using static Vanara.PInvoke.User32; @@ -408,118 +409,5 @@ namespace Vanara.Windows.Shell AddClipboardFormatListener(MessageWindowHandle); } } - - private class SafeMoveableHGlobalHandle : SafeHandle - { - private static readonly IMemoryMethods mm = Kernel32.MoveableHGlobalMemoryMethods.Instance; - private SizeT sz; - - /// Initializes a new instance of the class. - /// The handle. - /// The size of memory allocated to the handle, in bytes. - /// if set to true if this class is responsible for freeing the memory on disposal. - public SafeMoveableHGlobalHandle(IntPtr handle, SizeT size, bool ownsHandle = true) : base(IntPtr.Zero, ownsHandle) - { - SetHandle(handle); - sz = size; - } - - /// Initializes a new instance of the class. - /// The size of memory to allocate, in bytes. - /// size - The value of this argument must be non-negative - public SafeMoveableHGlobalHandle(SizeT size) : base(IntPtr.Zero, true) - { - if (size == 0) return; - RuntimeHelpers.PrepareConstrainedRegions(); - SetHandle(mm.AllocMem(sz = size)); - } - - /// - /// Allocates from unmanaged memory to represent an array of pointers and marshals the unmanaged pointers (IntPtr) to the native - /// array equivalent. - /// - /// Array of unmanaged pointers - /// SafeMoveableHGlobalHandle object to an native (unmanaged) array of pointers - public SafeMoveableHGlobalHandle(byte[] bytes) : this(bytes?.Length ?? 0) - { - if (sz == 0) return; - CallLocked(p => Marshal.Copy(bytes, 0, p, sz)); - } - - /// Represents a NULL memory pointer. - public static SafeMoveableHGlobalHandle Null { get; } = new SafeMoveableHGlobalHandle(IntPtr.Zero, 0, false); - - public override bool IsInvalid => handle == IntPtr.Zero; - - /// Gets or sets the size in bytes of the allocated memory block. - /// The size in bytes of the allocated memory block. - public SizeT Size - { - get => sz; - set - { - if (value == 0) - { - ReleaseHandle(); - } - else - { - RuntimeHelpers.PrepareConstrainedRegions(); - handle = IsInvalid ? mm.AllocMem(value) : mm.ReAllocMem(handle, value); - sz = value; - } - } - } - - /// - /// Allocates from unmanaged memory to represent a structure with a variable length array at the end and marshal these structure - /// elements. It is the callers responsibility to marshal what precedes the trailing array into the unmanaged memory. ONLY - /// structures with attribute StructLayout of LayoutKind.Sequential are supported. - /// - /// Type of the trailing array of structures - /// Collection of structure objects - /// Number of bytes preceding the trailing array of structures - /// object to an native (unmanaged) structure with a trail array of structures - public static SafeMoveableHGlobalHandle CreateFromList(IEnumerable values, int prefixBytes = 0) => - new(InteropExtensions.MarshalToPtr(values, mm.AllocMem, out int s, prefixBytes, mm.LockMem, mm.UnlockMem), s); - - /// Allocates from unmanaged memory sufficient memory to hold an array of strings. - /// The list of strings. - /// The packing type for the strings. - /// The character set to use for the strings. - /// Number of bytes preceding the trailing strings. - /// - /// object to an native (unmanaged) array of strings stored using the model and the character set defined by . - /// - public static SafeMoveableHGlobalHandle CreateFromStringList(IEnumerable values, StringListPackMethod packing = StringListPackMethod.Concatenated, CharSet charSet = CharSet.Auto, int prefixBytes = 0) => - new(InteropExtensions.MarshalToPtr(values, packing, mm.AllocMem, out int s, charSet, prefixBytes, mm.LockMem, mm.UnlockMem), s); - - /// Allocates from unmanaged memory sufficient memory to hold an object of type T. - /// Native type - /// The value. - /// object to an native (unmanaged) memory block the size of T. - public static SafeMoveableHGlobalHandle CreateFromStructure(in T value = default) => - new(InteropExtensions.MarshalToPtr(value, mm.AllocMem, out int s, 0, mm.LockMem, mm.UnlockMem), s); - - /// Converts an to a where it owns the reference. - /// The . - /// The result of the conversion. - public static implicit operator SafeMoveableHGlobalHandle(IntPtr ptr) => new(ptr, 0, true); - - protected void CallLocked(Action action) - { - try { action.Invoke(mm.LockMem(handle)); } - finally { mm.UnlockMem(handle); } - } - - protected override bool ReleaseHandle() - { - mm.FreeMem(handle); - sz = 0; - handle = IntPtr.Zero; - return true; - } - } } } \ No newline at end of file