Function Hooking
var kernel32 = NativeLibrary.Load("kernel32.dll");
try
{
using var manager = new PageCodeManager();
var setThreadDescription = (delegate* unmanaged[Stdcall]<nint, char*, int>)NativeLibrary.GetExport(
kernel32, "SetThreadDescription");
var setThreadDescriptionHook = (delegate* unmanaged[Stdcall]<nint, char*, int>)&SetThreadDescriptionHook;
using var hook = FunctionHook.Create(manager, setThreadDescription, setThreadDescriptionHook, "bar");
hook.IsActive = true;
fixed (char* ptr = "foo")
_ = setThreadDescription(-1, ptr);
}
finally
{
NativeLibrary.Free(kernel32);
}
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
static int SetThreadDescriptionHook(nint hThread, char* lpThreadDescription)
{
Console.WriteLine(new string(lpThreadDescription) + FunctionHook.Current.State);
return 0;
}Hook Gate
Last updated