MinHook - API Hooking Library

Post your working scripts, libraries and tools
tmplinshi
Posts: 1306
Joined: 01 Oct 2013, 14:57

MinHook - API Hooking Library

11 Aug 2018, 06:59

An AHK wrapper for MinHook.

Example 1 - Hook MessageBoxW

Code: Select all

#Include <MinHook>

hook1 := New MinHook("user32.dll", "MessageBoxW", "MessageBoxW_Hook")

hook1.Enable()
MsgBox, 64, Hello, World

hook1.Disable()
MsgBox, 64, Hello, I'm back.

hook1 := "" ; Remove hook

MessageBoxW_Hook(hWnd, lpText, lpCaption, uType) {
	global hook1
	return DllCall(hook1.original, "ptr", hWnd, "str", "Hooked!", "ptr", lpCaption, "uint", 48)
}
Example 2 - Set value for input file type in webbrowser control
Example 3 - Hook another process using HotkeyIt's InjectAhkDll and AutoHotkey.dll
Example 4 - InjectAhkDll (hook address).ahk

Download
Last edited by tmplinshi on 10 Feb 2019, 12:18, edited 1 time in total.
burque505
Posts: 862
Joined: 22 Jan 2017, 19:37

Re: MinHook - API Hooking Library

11 Aug 2018, 14:56

Hi, tmplinshi, how do I handle

Code: Select all

#Include <MinHook>
? I can see that AutoHotkey_H lists its license on the github page, and found a program or two that use the DLL, but I don't find a library MinHook.ahk anywhere.
Edit: :facepalm: Wow. I don't know how much more obvious it could have been. :facepalm:
Sorry.
Regards,
burque505
Edit: That's really classy, tmplinshi, thank you. That will doubtless come in handy. I had to pull _Struct.ahk and sizeof.ahk out of the Lib folder because I already had them in my main Lib folder. Everything works great, ex. 3 works with AHK_H 1.1.29.01 v002.
Thanks again!
guest3456
Posts: 2497
Joined: 09 Oct 2013, 10:31

Re: MinHook - API Hooking Library

13 Aug 2018, 23:41

excellent

tmplinshi
Posts: 1306
Joined: 01 Oct 2013, 14:57

Update to v1.1 (2018-8-16)

16 Aug 2018, 01:29

  • The module dll will now unload when the hook removed. (Except for standard DLL - User32.dll, Kernel32.dll, ComCtl32.dll, or Gdi32.dll)
  • Added more directories to search for MinHook.dll. You can place Minhook.dll in one of the following directories:
    • WorkingDir
    • WorkingDir\MinHook\x32 and/or WorkingDir\MinHook\x64
    • Minhook.ahk Directory
    • Minhook.ahk Directory\MinHook\x32 and/or Minhook.ahk Directory\MinHook\x64
feiyue
Posts: 140
Joined: 08 Aug 2014, 04:08

Re: MinHook - API Hooking Library

16 Aug 2018, 03:56

Hi tmplinshi, This is a very useful script! thanks for sharing! :wave:

I think Example2 needs to modify one line (Otherwise I can't see the effect.):

Example2 - GetOpenFileNameW.ahk
return DllCall(hook1.original, "ptr", ofn), hook1 := ""
tmplinshi
Posts: 1306
Joined: 01 Oct 2013, 14:57

Re: MinHook - API Hooking Library

16 Aug 2018, 06:46

feiyue wrote:I think Example2 needs to modify one line (Otherwise I can't see the effect.):

Example2 - GetOpenFileNameW.ahk
return DllCall(hook1.original, "ptr", ofn), hook1 := ""
But then a window will popup, which was the Example2 trying to avoid. :)
malcev
Posts: 158
Joined: 12 Aug 2014, 12:37

Re: MinHook - API Hooking Library

27 Jan 2019, 19:25

tmplinshi, do not You know how can I hook directx endscene method, as it written here:
http://spazzarama.com/2010/03/29/screen-capture-with-direct3d-api-hooks
tmplinshi
Posts: 1306
Joined: 01 Oct 2013, 14:57

Re: MinHook - API Hooking Library

29 Jan 2019, 07:01

@malcev too complex for me.
malcev
Posts: 158
Joined: 12 Aug 2014, 12:37

Re: MinHook - API Hooking Library

30 Jan 2019, 06:33

I have done like this, I can hook endscene method but program hangs after that.
Do not know why.
Here is 32 bit program (do not need install):
https://files.fm/u/xast7cz4
Inject autohotkey.dll like this:

Code: Select all

f11::
WinGet, PID, PID, A
FileRead,HookScript,minhook.ahk
rThread:=""
rThread:=InjectAhkDll(PID,A_ScriptDir "\AutoHotkey.dll", HookScript)
return
minhook.ahk:

Code: Select all

#Persistent
SetWorkingDir %A_ScriptDir%
d3d := Direct3DCreate9(D3D_SDK_VERSION := 32) 
VarSetCapacity(D3DPRESENT_PARAMETERS, 48+2*A_PtrSize, 0) 
NumPut(1, D3DPRESENT_PARAMETERS, 0)   ; BackBufferWidth
NumPut(1, D3DPRESENT_PARAMETERS, 4)   ; BackBufferHeight
NumPut(1, D3DPRESENT_PARAMETERS, 24)   ; D3DSWAPEFFECT_DISCARD
NumPut(1, D3DPRESENT_PARAMETERS, 24+2*A_PtrSize)   ; Windowed
IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT := 0, D3DDEVTYPE_HAL := 1, 0, D3DCREATE_HARDWARE_VERTEXPROCESSING := 0x00000040, &D3DPRESENT_PARAMETERS, device)
EndScene := NumGet(NumGet(device+0)+42*A_PtrSize) 
Release(device)
Release(d3d)

MinhookDll := "MinHook.dll"
MinHook_Init(MinhookDll)
MH_CreateHook(EndScene, cbAddr := RegisterCallback("EndScene_Hook", "F"))
MH_EnableHook()
return

EndScene_Hook(device)
{
   msgbox hook
}


Direct3DCreate9(SDKVersion)
{
   if !DllCall("GetModuleHandle","str","d3d9", "PTR")
   {
      MsgBox, 16, Error, d3d9 failed.
      ExitApp
   }
   return DllCall("d3d9\Direct3DCreate9", "uint", SDKVersion)
}

IDirect3D9_CreateDevice(this,Adapter,DeviceType,hFocusWindow,BehaviorFlags,pPresentationParameters,ByRef ppReturnedDeviceInterface)
{
   hr := DllCall(NumGet(NumGet(this+0)+16*A_PtrSize),"ptr",this,"uint",Adapter,"uint",DeviceType,"ptr",hFocusWindow,"uint",BehaviorFlags,"ptr",pPresentationParameters,"ptr*",ppReturnedDeviceInterface)
   if hr
      _Error(A_ThisFunc " error: " hr "`nErrorLevel: " ErrorLevel)
}

Release(this)
{
   return DllCall(NumGet(NumGet(this+0)+2*A_PtrSize), "ptr", this)
}

_Error(val)
{
   msgbox % val
   ExitApp
}


MinHook_Init(MinhookDll)
{
   Static h
   If Not h
   {
      h:=DllCall("LoadLibrary","Str",MinhookDll, "Ptr")
      MH_Initialize()
   }
}

; Creates a Hook for the specified target function, in disabled state.
; Parameters:
;   pTarget    [in]  A pointer to the target function, which will be
;                    overridden by the detour function.
;   pDetour    [in]  A pointer to the detour function, which will override
;                    the target function.
;   ppOriginal [out] A pointer to the trampoline function, which will be
;                    used to call the original target function.
;                    This parameter can be NULL.
MH_CreateHook(pTarget, pDetour, ByRef ppOriginal := 0) {
	return DllCall("MinHook\MH_CreateHook"
	               , "ptr", pTarget
	               , "ptr", pDetour
	               , "uptr*", ppOriginal )
}

; Initialize the MinHook library. You must call this function EXACTLY ONCE
; at the beginning of your program.
MH_Initialize() {
	return DllCall("MinHook\MH_Initialize")
}

; Uninitialize the MinHook library. You must call this function EXACTLY
; ONCE at the end of your program.
MH_Uninitialize() {
	return DllCall("MinHook\MH_Uninitialize")
}

; Removes an already created hook.
; Parameters:
;   pTarget [in] A pointer to the target function.
MH_RemoveHook(pTarget) {
	return DllCall("MinHook\MH_RemoveHook", "ptr", pTarget)
}

/*
	#define MH_ALL_HOOKS NULL
*/

; Enables an already created hook.
; Parameters:
;   pTarget [in] A pointer to the target function.
;                If this parameter is MH_ALL_HOOKS, all created hooks are
;                enabled in one go.
MH_EnableHook(pTarget := 0) {
	return DllCall("MinHook\MH_EnableHook", "ptr", pTarget)
}

; Disables an already created hook.
; Parameters:
;   pTarget [in] A pointer to the target function.
;                If this parameter is MH_ALL_HOOKS, all created hooks are
;                disabled in one go.
MH_DisableHook(pTarget := 0) {
	return DllCall("MinHook\MH_DisableHook", "ptr", pTarget)
}
Galaxis
Posts: 21
Joined: 04 Feb 2016, 20:09

Re: MinHook - API Hooking Library

07 Feb 2019, 17:42

tmplinshi wrote:
11 Aug 2018, 06:59
An AHK wrapper for MinHook.

Example 1 - Hook MessageBoxW

Code: Select all

#Include <MinHook>

hook1 := New MinHook("user32.dll", "MessageBoxW", "MessageBoxW_Hook")

hook1.Enable()
MsgBox, 64, Hello, World

hook1.Disable()
MsgBox, 64, Hello, I'm back.

hook1 := "" ; Remove hook

MessageBoxW_Hook(hWnd, lpText, lpCaption, uType) {
	global hook1
	return DllCall(hook1.original, "ptr", hWnd, "str", "Hooked!", "ptr", lpCaption, "uint", 48)
}

Can you provide an example of hooking a function in a FPS game (ie. player ammo) ?
Or an example of hooking a specific pointer address in game memory.
Or finds what writes to this address, like in Cheat Engine.
Galaxis
Posts: 21
Joined: 04 Feb 2016, 20:09

Re: MinHook - API Hooking Library

10 Feb 2019, 11:01

Galaxis wrote:
07 Feb 2019, 17:42
tmplinshi wrote:
11 Aug 2018, 06:59
An AHK wrapper for MinHook.

Example 1 - Hook MessageBoxW

Code: Select all

#Include <MinHook>

hook1 := New MinHook("user32.dll", "MessageBoxW", "MessageBoxW_Hook")

hook1.Enable()
MsgBox, 64, Hello, World

hook1.Disable()
MsgBox, 64, Hello, I'm back.

hook1 := "" ; Remove hook

MessageBoxW_Hook(hWnd, lpText, lpCaption, uType) {
	global hook1
	return DllCall(hook1.original, "ptr", hWnd, "str", "Hooked!", "ptr", lpCaption, "uint", 48)
}

Can you provide an example of hooking a function in a FPS game (ie. player ammo) ?
Or an example of hooking a specific pointer address in game memory.
tmplinshi
Posts: 1306
Joined: 01 Oct 2013, 14:57

Re: MinHook - API Hooking Library

10 Feb 2019, 12:23

Hi Galaxis, i'm not sure but see "Example 4" that i just added.
Galaxis
Posts: 21
Joined: 04 Feb 2016, 20:09

Re: MinHook - API Hooking Library

21 Feb 2019, 07:56

malcev wrote:
27 Jan 2019, 19:25
tmplinshi, do not You know how can I hook directx endscene method, as it written here:
http://spazzarama.com/2010/03/29/screen-capture-with-direct3d-api-hooks
I want to hook DX11 =)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: chyraz and 57 guests