From 9275c25475f29b6cfad9042c923cff3c6bf5f3a4 Mon Sep 17 00:00:00 2001 From: 0x5BFA <62196528+0x5bfa@users.noreply.github.com> Date: Sat, 21 Sep 2024 07:58:28 +0000 Subject: [PATCH 1/3] Init --- .../Windows.Win32.ComHeapPtr.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/Files.App.CsWin32/Windows.Win32.ComHeapPtr.cs diff --git a/src/Files.App.CsWin32/Windows.Win32.ComHeapPtr.cs b/src/Files.App.CsWin32/Windows.Win32.ComHeapPtr.cs new file mode 100644 index 000000000000..12c7b17ab2a3 --- /dev/null +++ b/src/Files.App.CsWin32/Windows.Win32.ComHeapPtr.cs @@ -0,0 +1,49 @@ +// Copyright (c) 2024 Files Community +// Licensed under the MIT License. See the LICENSE. + +using System; +using System.Runtime.CompilerServices; +using Windows.Win32; +using Windows.Win32.System.Com; + +namespace Windows.Win32 +{ + /// + /// Contains a COM heap-allocated pointer and a set of methods to work with the pointer safely. + /// + public unsafe struct ComHeapPtr : IDisposable where T : unmanaged + { + private T* _ptr; + + public bool IsNull + => _ptr == default; + + public ComHeapPtr(T* ptr) + { + _ptr = ptr; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T* Get() + { + return _ptr; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly T** GetAddressOf() + { + return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Dispose() + { + T* ptr = _ptr; + if (ptr is not null) + { + _ptr = null; + PInvoke.CoTaskMemFree((void*)ptr); + } + } + } +} From 2d7e590ee7650a0f79a5951d3769e8e1a3c029d2 Mon Sep 17 00:00:00 2001 From: 0x5BFA <62196528+0x5bfa@users.noreply.github.com> Date: Sat, 21 Sep 2024 08:13:24 +0000 Subject: [PATCH 2/3] Fixed a missing declaration --- src/Files.App.CsWin32/NativeMethods.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index 9d7c337d7c4d..3760a851bb2c 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -133,3 +133,4 @@ IFileOperation IShellItem2 PSGetPropertyKeyFromName ShellExecuteEx +CoTaskMemFree From 9cda552c3e6bd75c3c8d73af624255db5b08bb5d Mon Sep 17 00:00:00 2001 From: 0x5BFA <62196528+0x5bfa@users.noreply.github.com> Date: Sat, 21 Sep 2024 19:51:09 +0900 Subject: [PATCH 3/3] Update Windows.Win32.ComHeapPtr.cs --- src/Files.App.CsWin32/Windows.Win32.ComHeapPtr.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Files.App.CsWin32/Windows.Win32.ComHeapPtr.cs b/src/Files.App.CsWin32/Windows.Win32.ComHeapPtr.cs index 12c7b17ab2a3..14f248ee7ff5 100644 --- a/src/Files.App.CsWin32/Windows.Win32.ComHeapPtr.cs +++ b/src/Files.App.CsWin32/Windows.Win32.ComHeapPtr.cs @@ -9,7 +9,7 @@ namespace Windows.Win32 { /// - /// Contains a COM heap-allocated pointer and a set of methods to work with the pointer safely. + /// Contains a heap pointer allocated via CoTaskMemAlloc and a set of methods to work with the pointer safely. /// public unsafe struct ComHeapPtr : IDisposable where T : unmanaged {