From bbc22f6646165bfe8ea2363d63eef8ee2cc6c276 Mon Sep 17 00:00:00 2001 From: David Hall Date: Tue, 4 Sep 2018 13:42:49 -0600 Subject: [PATCH] Changed calls from conversion operator to DangerousGetHandle for all GenericSafeHandle derivatives. --- PInvoke/Kernel32/LibLoaderApi.cs | 3 ++- PInvoke/Kernel32/WinCon.cs | 2 +- UnitTests/Core/InteropServices/GenericSafeHandleTests.cs | 2 +- UnitTests/PInvoke/Kernel32/Kernel32Tests.cs | 8 ++++---- Windows.Shell/ShellItem.cs | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/PInvoke/Kernel32/LibLoaderApi.cs b/PInvoke/Kernel32/LibLoaderApi.cs index 4800d0a3..1330dafa 100644 --- a/PInvoke/Kernel32/LibLoaderApi.cs +++ b/PInvoke/Kernel32/LibLoaderApi.cs @@ -1789,7 +1789,8 @@ namespace Vanara.PInvoke var hLib = LoadLibraryEx(fileName, IntPtr.Zero, flags); if (hLib.IsInvalid) throw new Win32Exception(); - SetHandle((IntPtr)hLib); + SetHandle(hLib.DangerousGetHandle()); + hLib.SetHandleAsInvalid(); } /// Initializes a new instance of the class. diff --git a/PInvoke/Kernel32/WinCon.cs b/PInvoke/Kernel32/WinCon.cs index fcd2441a..59b44176 100644 --- a/PInvoke/Kernel32/WinCon.cs +++ b/PInvoke/Kernel32/WinCon.cs @@ -932,7 +932,7 @@ namespace Vanara.PInvoke cnt = cnt << 1; var h = new SafeProcessHeapBlockHandle((uint)Marshal.SizeOf(typeof(uint)) * cnt); var c = GetConsoleProcessList(h, cnt); - if (c >= 1 && c <= cnt) return ((IntPtr)h).ToArray((int)c); + if (c >= 1 && c <= cnt) return h.DangerousGetHandle().ToArray((int)c); } while (cnt > 0); throw Win32Error.GetLastError().GetException(); } diff --git a/UnitTests/Core/InteropServices/GenericSafeHandleTests.cs b/UnitTests/Core/InteropServices/GenericSafeHandleTests.cs index fdc38ac3..e076fc6d 100644 --- a/UnitTests/Core/InteropServices/GenericSafeHandleTests.cs +++ b/UnitTests/Core/InteropServices/GenericSafeHandleTests.cs @@ -29,7 +29,7 @@ namespace Vanara.InteropServices.Tests { var h = new GenericSafeHandle(p => true); Assert.That(h.IsInvalid && h.DangerousGetHandle() == IntPtr.Zero); - Assert.That(IntPtr.Zero.Equals((IntPtr)h)); + Assert.That(IntPtr.Zero.Equals(h.DangerousGetHandle())); h.Dispose(); } diff --git a/UnitTests/PInvoke/Kernel32/Kernel32Tests.cs b/UnitTests/PInvoke/Kernel32/Kernel32Tests.cs index b55f0d05..cb294de6 100644 --- a/UnitTests/PInvoke/Kernel32/Kernel32Tests.cs +++ b/UnitTests/PInvoke/Kernel32/Kernel32Tests.cs @@ -385,15 +385,15 @@ namespace Vanara.PInvoke.Tests { var ph = new SafeProcessHeapBlockHandle(512); var fw = new WIN32_FIND_DATA {ftCreationTime = DateTime.Today.ToFileTimeStruct(), cFileName = "test.txt", dwFileAttributes = FileAttributes.Normal}; - Marshal.StructureToPtr(fw, (IntPtr)ph, false); - Assert.That(Marshal.ReadInt32((IntPtr)ph), Is.EqualTo((int)FileAttributes.Normal)); + Marshal.StructureToPtr(fw, ph.DangerousGetHandle(), false); + Assert.That(Marshal.ReadInt32(ph.DangerousGetHandle()), Is.EqualTo((int)FileAttributes.Normal)); Assert.That(ph.Size, Is.EqualTo(512)); using (var hh = HeapCreate(0, IntPtr.Zero, IntPtr.Zero)) { var hb = hh.GetBlock(512); - Marshal.StructureToPtr(fw, (IntPtr)hb, false); - Assert.That(Marshal.ReadInt32((IntPtr)hb), Is.EqualTo((int) FileAttributes.Normal)); + Marshal.StructureToPtr(fw, hb.DangerousGetHandle(), false); + Assert.That(Marshal.ReadInt32(hb.DangerousGetHandle()), Is.EqualTo((int) FileAttributes.Normal)); Assert.That(hb.Size, Is.EqualTo(512)); } } diff --git a/Windows.Shell/ShellItem.cs b/Windows.Shell/ShellItem.cs index 8800bfc3..51c498c7 100644 --- a/Windows.Shell/ShellItem.cs +++ b/Windows.Shell/ShellItem.cs @@ -586,7 +586,7 @@ namespace Vanara.Windows.Shell throw new InvalidOperationException("Thumbnails are not supported by this item."); hres.ThrowIfFailed(); //Marshal.ReleaseComObject(fctry); - return GetTransparentBitmap((IntPtr)hbitmap); + return GetTransparentBitmap(hbitmap.DangerousGetHandle()); } if (!flags.IsFlagSet(ShellItemGetImageOptions.IconOnly)) return GetThumbnail(size.Width);