From 860879abadb452923acbf3684625da06e3d5dae0 Mon Sep 17 00:00:00 2001 From: Stephen Monaco <1782158+stevemonaco@users.noreply.github.com> Date: Sun, 15 Dec 2024 05:44:23 -0500 Subject: [PATCH] Fix KnownFolder for x86 Windows (#17705) * Fix KnownFolder for x86 Windows * Remove try-finally --- .../Storage/FileIO/BclStorageProvider.cs | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs index d5434455b0..c14e8803d0 100644 --- a/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs +++ b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs @@ -150,29 +150,24 @@ internal abstract class BclStorageProvider : IStorageProvider private static unsafe string? TryGetWindowsKnownFolder(Guid guid) { - nint path = IntPtr.Zero; + char* path = null; string? result = null; - try + var hr = SHGetKnownFolderPath(&guid, 0, null, &path); + if (hr == 0) { - var hr = SHGetKnownFolderPath(guid, 0, 0, &path); - if (hr == 0) - { - result = Marshal.PtrToStringUni(path); - } + result = Marshal.PtrToStringUni((IntPtr)path); } - finally + + if (path != null) { - if (path != IntPtr.Zero) - { - Marshal.FreeCoTaskMem(path); - } + Marshal.FreeCoTaskMem((IntPtr)path); } return result; } private static readonly Guid s_folderDownloads = new Guid("374DE290-123F-4565-9164-39C4925E467B"); - [DllImport("shell32.dll")] - private static unsafe extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid id, int flags, nint token, nint* ppszPath); + [DllImport("shell32.dll", ExactSpelling = true)] + private static unsafe extern int SHGetKnownFolderPath(Guid* rfid, uint dwFlags, void* hToken, char** ppszPath); }