mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
feat: inject exception handler dll after EasyHook
This commit is contained in:
parent
e2ac9ddd43
commit
2ee990827b
3 changed files with 187 additions and 44 deletions
|
|
@ -1,8 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
|
|
@ -29,7 +32,7 @@ namespace Dalamud.Injector {
|
|||
};
|
||||
|
||||
|
||||
var pid = -1;
|
||||
var pid = -1;
|
||||
if (args.Length == 1) {
|
||||
pid = int.Parse(args[0]);
|
||||
}
|
||||
|
|
@ -69,6 +72,9 @@ namespace Dalamud.Injector {
|
|||
|
||||
// Inject to process
|
||||
Inject(process, startInfo);
|
||||
|
||||
// Inject exception handler
|
||||
NativeInject(process);
|
||||
}
|
||||
|
||||
private static void Inject(Process process, DalamudStartInfo info) {
|
||||
|
|
@ -86,6 +92,63 @@ namespace Dalamud.Injector {
|
|||
Console.WriteLine("Injected");
|
||||
}
|
||||
|
||||
private static void NativeInject(Process process) {
|
||||
var libPath = Path.GetFullPath("DalamudDebugStub.dll");
|
||||
|
||||
var handle = NativeFunctions.OpenProcess(
|
||||
NativeFunctions.ProcessAccessFlags.All,
|
||||
false,
|
||||
process.Id);
|
||||
|
||||
if (handle == IntPtr.Zero)
|
||||
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not OpenProcess");
|
||||
|
||||
var dllMem = NativeFunctions.VirtualAllocEx(
|
||||
handle,
|
||||
IntPtr.Zero,
|
||||
libPath.Length,
|
||||
NativeFunctions.AllocationType.Reserve | NativeFunctions.AllocationType.Commit,
|
||||
NativeFunctions.MemoryProtection.ExecuteReadWrite);
|
||||
|
||||
if (dllMem == IntPtr.Zero)
|
||||
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not alloc memory");
|
||||
|
||||
var pathBytes = Encoding.ASCII.GetBytes(libPath);
|
||||
if (!NativeFunctions.WriteProcessMemory(
|
||||
handle,
|
||||
dllMem,
|
||||
pathBytes,
|
||||
pathBytes.Length,
|
||||
out var bytesread
|
||||
))
|
||||
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not write DLL");
|
||||
|
||||
var kernel32 = NativeFunctions.GetModuleHandle("Kernel32.dll");
|
||||
var loadLibA = NativeFunctions.GetProcAddress(kernel32, "LoadLibraryA");
|
||||
|
||||
var remoteThread = NativeFunctions.CreateRemoteThread(
|
||||
handle,
|
||||
IntPtr.Zero,
|
||||
0,
|
||||
loadLibA,
|
||||
dllMem,
|
||||
0,
|
||||
IntPtr.Zero
|
||||
);
|
||||
|
||||
if (remoteThread == IntPtr.Zero)
|
||||
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not alloc memory");
|
||||
|
||||
NativeFunctions.VirtualFreeEx(
|
||||
handle,
|
||||
dllMem,
|
||||
0,
|
||||
NativeFunctions.AllocationType.Release);
|
||||
|
||||
NativeFunctions.CloseHandle(remoteThread);
|
||||
NativeFunctions.CloseHandle(handle);
|
||||
}
|
||||
|
||||
private static DalamudStartInfo GetDefaultStartInfo() {
|
||||
var ffxivDir = Path.GetDirectoryName(process.MainModule.FileName);
|
||||
var startInfo = new DalamudStartInfo {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue