DllInject.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Mir2Assistant.Common.Functions;
  2. using Mir2Assistant.Common.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace Mir2Assistant
  12. {
  13. public class DllInject
  14. {
  15. public enum HookType : int
  16. {
  17. WH_JOURNALRECORD = 0,
  18. WH_JOURNALPLAYBACK = 1,
  19. WH_KEYBOARD = 2,
  20. WH_GETMESSAGE = 3,
  21. WH_CALLWNDPROC = 4,
  22. WH_CBT = 5,
  23. WH_SYSMSGFILTER = 6,
  24. WH_MOUSE = 7,
  25. WH_HARDWARE = 8,
  26. WH_DEBUG = 9,
  27. WH_SHELL = 10,
  28. WH_FOREGROUNDIDLE = 11,
  29. WH_CALLWNDPROCRET = 12,
  30. WH_KEYBOARD_LL = 13,
  31. WH_MOUSE_LL = 14
  32. }
  33. [DllImport("kernel32.dll")]
  34. public static extern IntPtr LoadLibrary(string dllToLoad);
  35. [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  36. static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  37. [DllImport("user32.dll", SetLastError = true)]
  38. static extern IntPtr SetWindowsHookEx(HookType hookType, IntPtr lpfn, IntPtr hMod, uint dwThreadId);
  39. [DllImport("user32.dll", SetLastError = true)]
  40. [return: MarshalAs(UnmanagedType.Bool)]
  41. static extern bool UnhookWindowsHookEx(IntPtr hhk);
  42. [DllImport("user32.dll", EntryPoint = "FindWindow")]
  43. private static extern int FindWindow(string sClass, string sWindow);
  44. [DllImport("kernel32.dll")]
  45. public static extern bool FreeLibrary(IntPtr hModule);
  46. [DllImport("kernel32.dll", SetLastError = true)]
  47. public static extern uint WaitForSingleObject(uint hHandle, uint dwMilliseconds);
  48. public static void SetHook(IntPtr addr, MirGameInstanceModel gi)
  49. {
  50. var handle = SetWindowsHookEx(HookType.WH_CALLWNDPROC, addr, gi.LibIpdl!.Value, gi.MirThreadId);
  51. if (handle == IntPtr.Zero)
  52. {
  53. MessageBox.Show("Injection failed to set hook.");
  54. return;
  55. }
  56. gi.HookHandle = handle;
  57. while (gi.LibIpdl > 0)
  58. {
  59. Thread.Sleep(100);
  60. }
  61. }
  62. public static void Unhook(MirGameInstanceModel gi)
  63. {
  64. SendMirCall.Send(gi, 9002, new nint[] { });
  65. if (gi.HookHandle != null && gi.HookHandle != IntPtr.Zero)
  66. {
  67. UnhookWindowsHookEx(gi.HookHandle!.Value);
  68. FreeLibrary(gi.LibIpdl!.Value);
  69. gi.LibIpdl = 0;
  70. }
  71. }
  72. public static void loadDll(MirGameInstanceModel gi)
  73. {
  74. IntPtr addr = (IntPtr)0;
  75. var ipdl = LoadLibrary(Path.Combine(Application.StartupPath, "Mir2HookCall.dll"));
  76. if (ipdl == IntPtr.Zero)
  77. {
  78. MessageBox.Show("Injection failed: could not load target DLL.");
  79. return;
  80. }
  81. addr = GetProcAddress(ipdl, "HookProc");
  82. if (addr == IntPtr.Zero)
  83. {
  84. MessageBox.Show("Injection failed: could not DLL entrypoint (did you read the documentation?).");
  85. return;
  86. }
  87. gi.LibIpdl = ipdl;
  88. var thread = new Thread(() => SetHook(addr, gi));
  89. thread.Start();
  90. }
  91. }
  92. }