diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs
index abe671d67..15c5c8cda 100644
--- a/Dalamud/Dalamud.cs
+++ b/Dalamud/Dalamud.cs
@@ -1,4 +1,6 @@
using System;
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
@@ -19,6 +21,7 @@ using Dalamud.Interface.Internal;
using Dalamud.IoC.Internal;
using Dalamud.Plugin.Internal;
using Dalamud.Plugin.Ipc.Internal;
+using HarmonyLib;
using Serilog;
using Serilog.Core;
@@ -52,6 +55,8 @@ namespace Dalamud
/// The Dalamud configuration.
public Dalamud(DalamudStartInfo info, LoggingLevelSwitch loggingLevelSwitch, ManualResetEvent finishSignal, DalamudConfiguration configuration)
{
+ this.ApplyProcessPatch();
+
Service.Set(this);
Service.Set(info);
Service.Set(configuration);
@@ -363,5 +368,34 @@ namespace Dalamud
var oldFilter = NativeFunctions.SetUnhandledExceptionFilter(releaseFilter);
Log.Debug("Reset ExceptionFilter, old: {0}", oldFilter);
}
+
+ ///
+ /// Patch method for the class Process.Handle. This patch facilitates fixing Reloaded so that it
+ /// uses pseudo-handles to access memory, to prevent permission errors.
+ /// It should never be called manually.
+ ///
+ /// The equivalent of `this`.
+ /// The result from the original method.
+ [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "Enforced naming for special injected parameters")]
+ private static void ProcessHandlePatch(Process __instance, ref IntPtr __result)
+ {
+ if (__instance.Id == Environment.ProcessId)
+ {
+ __result = (IntPtr)0xFFFFFFFF;
+ }
+
+ Log.Verbose($"Process.Handle // {__instance.ProcessName} // {__result:X}");
+ }
+
+ private void ApplyProcessPatch()
+ {
+ var harmony = new Harmony("goatcorp.dalamud");
+
+ var targetType = typeof(Process);
+
+ var handleTarget = AccessTools.PropertyGetter(targetType, nameof(Process.Handle));
+ var handlePatch = AccessTools.Method(typeof(Dalamud), nameof(Dalamud.ProcessHandlePatch));
+ harmony.Patch(handleTarget, postfix: new(handlePatch));
+ }
}
}