Remove SHGetMalloc usage, use Marshal.FreeCoTaskMem instead
Marshal.FreeCoTaskMem can be used instead (assuming we're >= Win2000), see https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetmalloc Also fix pszDisplayName allocation (needs * sizeof(char) due to unicode funny)
This commit is contained in:
parent
956af3577e
commit
3bc44d6756
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
|
||||
|
@ -59,28 +58,28 @@ namespace BizHawk.Client.EmuHawk
|
|||
return DialogResult.Cancel;
|
||||
}
|
||||
|
||||
var browseOptions = BrowseOptions;
|
||||
if (ApartmentState.MTA == Application.OleRequired())
|
||||
{
|
||||
browseOptions &= ~BROWSEINFOW.FLAGS.NewDialogStyle;
|
||||
}
|
||||
|
||||
var pidlRet = IntPtr.Zero;
|
||||
var pszDisplayName = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
var buffer = Marshal.AllocHGlobal(Win32Imports.MAX_PATH);
|
||||
var browseOptions = BrowseOptions;
|
||||
if (ApartmentState.MTA == Application.OleRequired())
|
||||
{
|
||||
browseOptions &= ~BROWSEINFOW.FLAGS.NewDialogStyle;
|
||||
}
|
||||
|
||||
pszDisplayName = Marshal.AllocCoTaskMem(Win32Imports.MAX_PATH * sizeof(char));
|
||||
var bi = new BROWSEINFOW
|
||||
{
|
||||
hwndOwner = hWndOwner,
|
||||
pidlRoot = pidlRoot,
|
||||
pszDisplayName = buffer,
|
||||
pszDisplayName = pszDisplayName,
|
||||
lpszTitle = Description,
|
||||
ulFlags = browseOptions,
|
||||
lpfn = Callback
|
||||
};
|
||||
|
||||
pidlRet = SHBrowseForFolderW(ref bi);
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
if (pidlRet == IntPtr.Zero)
|
||||
{
|
||||
return DialogResult.Cancel; // user clicked Cancel
|
||||
|
@ -96,13 +95,9 @@ namespace BizHawk.Client.EmuHawk
|
|||
}
|
||||
finally
|
||||
{
|
||||
_ = SHGetMalloc(out var malloc);
|
||||
malloc.Free(pidlRoot);
|
||||
|
||||
if (pidlRet != IntPtr.Zero)
|
||||
{
|
||||
malloc.Free(pidlRet);
|
||||
}
|
||||
Marshal.FreeCoTaskMem(pidlRoot);
|
||||
Marshal.FreeCoTaskMem(pidlRet);
|
||||
Marshal.FreeCoTaskMem(pszDisplayName);
|
||||
}
|
||||
|
||||
return DialogResult.OK;
|
||||
|
|
|
@ -39,7 +39,7 @@ namespace BizHawk.Common
|
|||
PS_DLL = 0x80000000
|
||||
}
|
||||
|
||||
[DllImport("Ole32.dll", ExactSpelling = true)]
|
||||
[DllImport("ole32.dll", ExactSpelling = true)]
|
||||
public static extern int CoCreateInstance(
|
||||
[In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
|
||||
IntPtr pUnkOuter,
|
||||
|
|
|
@ -55,35 +55,9 @@ namespace BizHawk.Common
|
|||
}
|
||||
}
|
||||
|
||||
[Guid("00000002-0000-0000-C000-000000000046")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
public interface IMalloc
|
||||
{
|
||||
[PreserveSig]
|
||||
IntPtr Alloc([In] int cb);
|
||||
|
||||
[PreserveSig]
|
||||
IntPtr Realloc([In] IntPtr pv, [In] int cb);
|
||||
|
||||
[PreserveSig]
|
||||
void Free([In] IntPtr pv);
|
||||
|
||||
[PreserveSig]
|
||||
int GetSize([In] IntPtr pv);
|
||||
|
||||
[PreserveSig]
|
||||
int DidAlloc(IntPtr pv);
|
||||
|
||||
[PreserveSig]
|
||||
void HeapMinimize();
|
||||
}
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
|
||||
public static extern IntPtr SHBrowseForFolderW(ref BROWSEINFOW bi);
|
||||
|
||||
[DllImport("shell32.dll", ExactSpelling = true)]
|
||||
public static extern int SHGetMalloc(out IMalloc ppMalloc);
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
|
||||
public static extern int SHGetPathFromIDListW(IntPtr pidl, char[] pszPath);
|
||||
|
||||
|
|
Loading…
Reference in New Issue