From 90831ea16f607f16dc7f6f863552d025026c7121 Mon Sep 17 00:00:00 2001 From: dahall Date: Sat, 22 Jan 2022 15:31:55 -0700 Subject: [PATCH] Changed SafeMoveableHGlobalHandle.ctor(IntPtr...) method to not accept size and rather extract it using GlobalSize --- PInvoke/Kernel32/SafeMoveableHGlobalHandle.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/PInvoke/Kernel32/SafeMoveableHGlobalHandle.cs b/PInvoke/Kernel32/SafeMoveableHGlobalHandle.cs index 4f25eeed..0739b686 100644 --- a/PInvoke/Kernel32/SafeMoveableHGlobalHandle.cs +++ b/PInvoke/Kernel32/SafeMoveableHGlobalHandle.cs @@ -16,9 +16,8 @@ namespace Vanara.PInvoke { /// 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(handle, size, ownsHandle) { } + public SafeMoveableHGlobalHandle(IntPtr handle, bool ownsHandle = true) : base(handle, handle == default ? 0 : GlobalSize(handle), ownsHandle) { } /// Initializes a new instance of the class. /// The size of memory to allocate, in bytes. @@ -49,7 +48,7 @@ namespace Vanara.PInvoke internal SafeMoveableHGlobalHandle() : base(0) { } /// Represents a NULL memory pointer. - public static SafeMoveableHGlobalHandle Null { get; } = new SafeMoveableHGlobalHandle(IntPtr.Zero, 0, false); + public static SafeMoveableHGlobalHandle Null { get; } = new SafeMoveableHGlobalHandle(IntPtr.Zero, false); /// /// Allocates from unmanaged memory to represent a structure with a variable length array at the end and marshal these structure @@ -61,7 +60,7 @@ namespace Vanara.PInvoke /// 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); + new(InteropExtensions.MarshalToPtr(values, mm.AllocMem, out _, prefixBytes, mm.LockMem, mm.UnlockMem), true); /// Allocates from unmanaged memory sufficient memory to hold an array of strings. /// The list of strings. @@ -73,19 +72,19 @@ namespace Vanara.PInvoke /// name="packing"/> 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); + CharSet charSet = CharSet.Auto, int prefixBytes = 0) => new(InteropExtensions.MarshalToPtr(values, packing, mm.AllocMem, out _, charSet, prefixBytes, mm.LockMem, mm.UnlockMem), true); /// 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); + new(InteropExtensions.MarshalToPtr(value, mm.AllocMem, out _, 0, mm.LockMem, mm.UnlockMem), true); /// 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); + public static implicit operator SafeMoveableHGlobalHandle(IntPtr ptr) => new(ptr, true); } } } \ No newline at end of file