Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Summpot committed Mar 23, 2024
1 parent 351b979 commit 88120ac
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 197 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "native/minhook/src"]
path = native/minhook/src
url = https://github.com/Summpot/minhook
url = https://github.com/TsudaKageyu/minhook
2 changes: 1 addition & 1 deletion native/minhook/src
249 changes: 70 additions & 179 deletions src/MinHook.SourceGenerators/HooksGenerator.cs

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions src/MinHook/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,28 @@ namespace MinHook;

public static class Hook
{
public static bool CheckLibraryLoaded(string libraryName)
{
var handle = Kernel32.GetModuleHandle(libraryName);
if (handle == IntPtr.Zero)
{
return false;
}
return true;
}

public static Hook<T> Create<T>(string moduleName, string funcName, IntPtr detour) where T : Delegate
{
var handle = Kernel32.GetModuleHandle(moduleName);
if (handle == IntPtr.Zero)
{
MinHookException.Throw(MinHookStatus.ErrorModuleNotFound);
}
var target = Kernel32.GetProcAddress(handle, funcName);
if (target == IntPtr.Zero)
{
MinHookException.Throw(MinHookStatus.ErrorModuleNotFound);
}
return new Hook<T>(target, detour);
}
public static Hook<T> Create<T>(string moduleName, string funcName, T detour) where T : Delegate
Expand Down Expand Up @@ -59,18 +77,13 @@ private void Initialize()
{
MinHookException.Throw(status);
}


}

public void Enable()
{
var status = Native.SetThreadFreezeMethod(MhThreadFreezeMethod.FastUndocumented);
if (status != MinHookStatus.Ok)
{
MinHookException.Throw(status);
}
status = Native.EnableHook(_target);
var status = Native.EnableHook(_target);
if (status != MinHookStatus.Ok && status != MinHookStatus.Enabled)
{
MinHookException.Throw(status);
Expand Down
4 changes: 1 addition & 3 deletions src/MinHook/Kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
namespace MinHook;
public static class Kernel32
{
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
public delegate IntPtr GetProcAddressDelegate(IntPtr hModule, string lpProcName);


[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);

Expand Down
91 changes: 91 additions & 0 deletions src/MinHook/LibraryLoadingMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace MinHook;
public class LibraryLoadingMonitor
{
[UnmanagedFunctionPointer(CallingConvention.StdCall, BestFitMapping = true, CharSet = CharSet.Ansi)]
public delegate IntPtr LoadLibraryADelegate(string lpLibFileName);

[UnmanagedFunctionPointer(CallingConvention.StdCall, BestFitMapping = true, CharSet = CharSet.Unicode)]
public delegate IntPtr LoadLibraryWDelegate(string lpLibFileName);

[UnmanagedFunctionPointer(CallingConvention.StdCall, BestFitMapping = true, CharSet = CharSet.Ansi)]
public delegate IntPtr LoadLibraryExADelegate(string lpLibFileName, IntPtr hFile, uint dwFlags);

[UnmanagedFunctionPointer(CallingConvention.StdCall, BestFitMapping = true, CharSet = CharSet.Unicode)]
public delegate IntPtr LoadLibraryExWDelegate(string lpLibFileName, IntPtr hFile, uint dwFlags);


private static Hook<LoadLibraryADelegate> s_hook1 = Hook.Create<LoadLibraryADelegate>("kernel32", "LoadLibraryA", Hook_LoadLibraryA);
private static Hook<LoadLibraryWDelegate> s_hook2 = Hook.Create<LoadLibraryWDelegate>("kernel32", "LoadLibraryW", Hook_LoadLibraryW);
private static Hook<LoadLibraryExADelegate> s_hook3 = Hook.Create<LoadLibraryExADelegate>("kernel32", "LoadLibraryExA", Hook_LoadLibraryExA);
private static Hook<LoadLibraryExWDelegate> s_hook4 = Hook.Create<LoadLibraryExWDelegate>("kernel32", "LoadLibraryExW", Hook_LoadLibraryExW);

public static bool Enabled { get; private set; }

private static IntPtr Hook_LoadLibraryA(string lpLibFileName)
{
OnLibraryLoading(lpLibFileName);
var handle = s_hook1.Original(lpLibFileName);
OnLibraryLoaded(handle);
return handle;
}

private static IntPtr Hook_LoadLibraryW(string lpLibFileName)
{
OnLibraryLoading(lpLibFileName);
var handle = s_hook2.Original(lpLibFileName);
OnLibraryLoaded(handle);
return handle;
}
private static IntPtr Hook_LoadLibraryExA(string lpLibFileName, IntPtr hFile, uint dwFlags)
{
OnLibraryLoading(lpLibFileName);
var handle = s_hook3.Original(lpLibFileName, hFile, dwFlags);
OnLibraryLoaded(handle);
return handle;
}
private static IntPtr Hook_LoadLibraryExW(string lpLibFileName, IntPtr hFile, uint dwFlags)
{
OnLibraryLoading(lpLibFileName);
var handle = s_hook4.Original(lpLibFileName, hFile, dwFlags);
OnLibraryLoaded(handle);
return handle;
}

public static void Enable()
{
s_hook1.Enable();
s_hook2.Enable();
s_hook3.Enable();
s_hook4.Enable();
}

public static void Disable()
{
s_hook1.Disable();
s_hook2.Disable();
s_hook3.Disable();
s_hook4.Disable();
}

private static void OnLibraryLoading(string libraryName)
{
LibraryLoading?.Invoke(null, new LibraryLoadingEventArgs(libraryName));
}

private static void OnLibraryLoaded(IntPtr handle)
{
LibraryLoaded?.Invoke(null, new LibraryLoadedEventArgs(handle));
}

public static event EventHandler<LibraryLoadingEventArgs>? LibraryLoading;
public static event EventHandler<LibraryLoadedEventArgs>? LibraryLoaded;

public record struct LibraryLoadingEventArgs(string LibraryName);
public record struct LibraryLoadedEventArgs(IntPtr Handle);
}
2 changes: 1 addition & 1 deletion tests/MinHook.Tests/TestLazyHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace MinHook.Tests;
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public delegate uint GetTickCountDelegate();

[StaticLazyHook<GetTickCountDelegate>("Kernel32", "GetTickCount")]
[StaticLazyHook<GetTickCountDelegate>("kernel32", "GetTickCount")]

internal partial class TestLazyHook
{
Expand Down
12 changes: 7 additions & 5 deletions tests/MinHook.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace MinHook.Tests;

public class UnitTest1
{

[DllImport("Kernel32.dll")]
[DllImport("kernel32.dll")]
public static extern uint GetTickCount();


[Fact]
public void Test1()
{
StaticLazyHookManager.Enable();
Assert.Equal(0u, GetTickCount());
StaticLazyHookManager.Disable(alsoDisableAllEnabledHooks: true);
Assert.NotEqual(0u, GetTickCount());
TestLazyHook.Enable();
Assert.Equal(0u,GetTickCount());
TestLazyHook.Disable();
Assert.NotEqual(0u,GetTickCount());
}
}

0 comments on commit 88120ac

Please sign in to comment.