Disable SafeHandles

This commit is contained in:
Haselnussbomber 2025-12-05 19:10:31 +01:00
parent 7cf20fe102
commit d94cacaac3
No known key found for this signature in database
GPG key ID: BB905BB49E7295D1
6 changed files with 28 additions and 22 deletions

View file

@ -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);

View file

@ -201,19 +201,19 @@ public abstract class Hook<T> : 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<T>(procAddress, detour, Assembly.GetCallingAssembly());
return new MinHookHook<T>(address, detour, Assembly.GetCallingAssembly());
else
return new ReloadedHook<T>(procAddress, detour, Assembly.GetCallingAssembly());
return new ReloadedHook<T>(address, detour, Assembly.GetCallingAssembly());
}
/// <summary>

View file

@ -1,4 +1,5 @@
{
"$schema": "https://aka.ms/CsWin32.schema.json",
"useSafeHandles": false,
"allowMarshaling": false
}

View file

@ -14,11 +14,11 @@ namespace Dalamud;
/// </remarks>
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();
}
/// <summary>
@ -30,7 +30,7 @@ public static class SafeMemory
/// <returns>Whether the read succeeded.</returns>
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
/// <returns>Whether the write succeeded.</returns>
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),

View file

@ -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();

View file

@ -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;
}