diff --git a/Dalamud/EntryPoint.cs b/Dalamud/EntryPoint.cs index 15077f3d8..b5504b046 100644 --- a/Dalamud/EntryPoint.cs +++ b/Dalamud/EntryPoint.cs @@ -263,7 +263,7 @@ public sealed class EntryPoint var symbolPath = Path.Combine(info.AssetDirectory, "UIRes", "pdb"); var searchPath = $".;{symbolPath}"; - var currentProcess = Windows.Win32.PInvoke.GetCurrentProcess_SafeHandle(); + var currentProcess = Windows.Win32.PInvoke.GetCurrentProcess(); // Remove any existing Symbol Handler and Init a new one with our search path added Windows.Win32.PInvoke.SymCleanup(currentProcess); diff --git a/Dalamud/Hooking/Hook.cs b/Dalamud/Hooking/Hook.cs index faf4658a5..1cd3ef91d 100644 --- a/Dalamud/Hooking/Hook.cs +++ b/Dalamud/Hooking/Hook.cs @@ -201,19 +201,19 @@ public abstract class Hook : IDalamudHook where T : Delegate if (EnvironmentConfiguration.DalamudForceMinHook) useMinHook = true; - using var moduleHandle = Windows.Win32.PInvoke.GetModuleHandle(moduleName); - if (moduleHandle.IsInvalid) + var moduleHandle = Windows.Win32.PInvoke.GetModuleHandle(moduleName); + if (moduleHandle.IsNull) throw new Exception($"Could not get a handle to module {moduleName}"); - var procAddress = (nint)Windows.Win32.PInvoke.GetProcAddress(moduleHandle, exportName); - if (procAddress == IntPtr.Zero) + var procAddress = Windows.Win32.PInvoke.GetProcAddress(moduleHandle, exportName); + if (procAddress.IsNull) throw new Exception($"Could not get the address of {moduleName}::{exportName}"); - procAddress = HookManager.FollowJmp(procAddress); + var address = HookManager.FollowJmp(procAddress.Value); if (useMinHook) - return new MinHookHook(procAddress, detour, Assembly.GetCallingAssembly()); + return new MinHookHook(address, detour, Assembly.GetCallingAssembly()); else - return new ReloadedHook(procAddress, detour, Assembly.GetCallingAssembly()); + return new ReloadedHook(address, detour, Assembly.GetCallingAssembly()); } /// diff --git a/Dalamud/NativeMethods.json b/Dalamud/NativeMethods.json index ffb313dfc..46fd3504f 100644 --- a/Dalamud/NativeMethods.json +++ b/Dalamud/NativeMethods.json @@ -1,4 +1,5 @@ { "$schema": "https://aka.ms/CsWin32.schema.json", + "useSafeHandles": false, "allowMarshaling": false } diff --git a/Dalamud/SafeMemory.cs b/Dalamud/SafeMemory.cs index 9a1af0625..ca0c8ff92 100644 --- a/Dalamud/SafeMemory.cs +++ b/Dalamud/SafeMemory.cs @@ -14,11 +14,11 @@ namespace Dalamud; /// public static class SafeMemory { - private static readonly SafeHandle Handle; + private static readonly HANDLE Handle; static SafeMemory() { - Handle = Windows.Win32.PInvoke.GetCurrentProcess_SafeHandle(); + Handle = Windows.Win32.PInvoke.GetCurrentProcess(); } /// @@ -30,7 +30,7 @@ public static class SafeMemory /// Whether the read succeeded. public static unsafe bool ReadBytes(IntPtr address, int count, out byte[] buffer) { - if (Handle.IsClosed || Handle.IsInvalid) + if (Handle.IsNull) { buffer = []; return false; @@ -41,7 +41,7 @@ public static class SafeMemory { UIntPtr bytesRead; if (!Windows.Win32.PInvoke.ReadProcessMemory( - (HANDLE)Handle.DangerousGetHandle(), + Handle, address.ToPointer(), p, new UIntPtr((uint)count), @@ -62,7 +62,7 @@ public static class SafeMemory /// Whether the write succeeded. public static unsafe bool WriteBytes(IntPtr address, byte[] buffer) { - if (Handle.IsClosed || Handle.IsInvalid) + if (Handle.IsNull) return false; if (buffer.Length == 0) @@ -72,7 +72,7 @@ public static class SafeMemory fixed (byte* p = buffer) { if (!Windows.Win32.PInvoke.WriteProcessMemory( - (HANDLE)Handle.DangerousGetHandle(), + Handle, address.ToPointer(), p, new UIntPtr((uint)buffer.Length), diff --git a/Dalamud/Utility/FilesystemUtil.cs b/Dalamud/Utility/FilesystemUtil.cs index 560e06da3..f1b62ee21 100644 --- a/Dalamud/Utility/FilesystemUtil.cs +++ b/Dalamud/Utility/FilesystemUtil.cs @@ -48,33 +48,39 @@ public static class FilesystemUtil // Open the temp file var tempPath = path + ".tmp"; - using var tempFile = Windows.Win32.PInvoke.CreateFile( + var tempFile = Windows.Win32.PInvoke.CreateFile( tempPath, (uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE), FILE_SHARE_MODE.FILE_SHARE_NONE, null, FILE_CREATION_DISPOSITION.CREATE_ALWAYS, FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL, - null); + HANDLE.Null); - if (tempFile.IsInvalid) + if (tempFile.IsNull) throw new Win32Exception(); // Write the data uint bytesWritten = 0; fixed (byte* ptr = bytes) { - if (!Windows.Win32.PInvoke.WriteFile((HANDLE)tempFile.DangerousGetHandle(), ptr, (uint)bytes.Length, &bytesWritten, null)) + if (!Windows.Win32.PInvoke.WriteFile(tempFile, ptr, (uint)bytes.Length, &bytesWritten, null)) throw new Win32Exception(); } if (bytesWritten != bytes.Length) + { + Windows.Win32.PInvoke.CloseHandle(tempFile); throw new Exception($"Could not write all bytes to temp file ({bytesWritten} of {bytes.Length})"); + } if (!Windows.Win32.PInvoke.FlushFileBuffers(tempFile)) + { + Windows.Win32.PInvoke.CloseHandle(tempFile); throw new Win32Exception(); + } - tempFile.Close(); + Windows.Win32.PInvoke.CloseHandle(tempFile); if (!Windows.Win32.PInvoke.MoveFileEx(tempPath, path, MOVE_FILE_FLAGS.MOVEFILE_REPLACE_EXISTING | MOVE_FILE_FLAGS.MOVEFILE_WRITE_THROUGH)) throw new Win32Exception(); diff --git a/Dalamud/Utility/Util.cs b/Dalamud/Utility/Util.cs index 19610ef64..f50efcf0d 100644 --- a/Dalamud/Utility/Util.cs +++ b/Dalamud/Utility/Util.cs @@ -858,7 +858,7 @@ public static partial class Util var sizeWithTerminators = pathBytesSize + (pathBytes.Length * 2); var dropFilesSize = sizeof(DROPFILES); - var hGlobal = Win32_PInvoke.GlobalAlloc_SafeHandle( + var hGlobal = Win32_PInvoke.GlobalAlloc( GLOBAL_ALLOC_FLAGS.GHND, // struct size + size of encoded strings + null terminator for each // string + two null terminators for end of list @@ -896,12 +896,11 @@ public static partial class Util { Win32_PInvoke.SetClipboardData( (uint)CLIPBOARD_FORMAT.CF_HDROP, - hGlobal); + (Windows.Win32.Foundation.HANDLE)hGlobal.Value); Win32_PInvoke.CloseClipboard(); return true; } - hGlobal.Dispose(); return false; }