diff --git a/Dalamud/SafeMemory.cs b/Dalamud/SafeMemory.cs index a8ac40a5d..9a1af0625 100644 --- a/Dalamud/SafeMemory.cs +++ b/Dalamud/SafeMemory.cs @@ -1,6 +1,8 @@ using System.Runtime.InteropServices; using System.Text; +using Windows.Win32.Foundation; + namespace Dalamud; /// @@ -28,12 +30,18 @@ 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) + { + buffer = []; + return false; + } + buffer = new byte[count <= 0 ? 0 : count]; fixed (byte* p = buffer) { UIntPtr bytesRead; if (!Windows.Win32.PInvoke.ReadProcessMemory( - Handle, + (HANDLE)Handle.DangerousGetHandle(), address.ToPointer(), p, new UIntPtr((uint)count), @@ -54,6 +62,9 @@ public static class SafeMemory /// Whether the write succeeded. public static unsafe bool WriteBytes(IntPtr address, byte[] buffer) { + if (Handle.IsClosed || Handle.IsInvalid) + return false; + if (buffer.Length == 0) return true; @@ -61,7 +72,7 @@ public static class SafeMemory fixed (byte* p = buffer) { if (!Windows.Win32.PInvoke.WriteProcessMemory( - Handle, + (HANDLE)Handle.DangerousGetHandle(), address.ToPointer(), p, new UIntPtr((uint)buffer.Length), diff --git a/Dalamud/Utility/FilesystemUtil.cs b/Dalamud/Utility/FilesystemUtil.cs index 3b4298b37..560e06da3 100644 --- a/Dalamud/Utility/FilesystemUtil.cs +++ b/Dalamud/Utility/FilesystemUtil.cs @@ -1,7 +1,8 @@ -using System.ComponentModel; +using System.ComponentModel; using System.IO; using System.Text; +using Windows.Win32.Foundation; using Windows.Win32.Storage.FileSystem; namespace Dalamud.Utility; @@ -61,8 +62,11 @@ public static class FilesystemUtil // Write the data uint bytesWritten = 0; - if (!Windows.Win32.PInvoke.WriteFile(tempFile, new ReadOnlySpan(bytes), &bytesWritten, null)) - throw new Win32Exception(); + fixed (byte* ptr = bytes) + { + if (!Windows.Win32.PInvoke.WriteFile((HANDLE)tempFile.DangerousGetHandle(), ptr, (uint)bytes.Length, &bytesWritten, null)) + throw new Win32Exception(); + } if (bytesWritten != bytes.Length) throw new Exception($"Could not write all bytes to temp file ({bytesWritten} of {bytes.Length})"); diff --git a/Directory.Packages.props b/Directory.Packages.props index 6c5070d35..58e355400 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -27,7 +27,7 @@ - +