1 (изменено: Malcev, 2019-01-30 07:00:14)

Тема: AHK: Перехват com интерфейса процесса

Есть програмка, хочу научиться перехватывать метод EndScene в directx9.
Делал вроде бы по этому алгоритму:
http://spazzarama.com/2010/03/29/screen … -api-hooks
Но при первом же перехвате, перехват происходит, но программа намертво виснет.
В чем может быть ошибка?
Программа 32 бит тут:
https://ru.files.fm/u/xast7cz4
Нужна библиотека MinHook.dll:
https://github.com/tmplinshi/MinHook-AH … inHook.dll
Инжектю через autohotkey.dll так:

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

Сам хук:

#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)
}

2 (изменено: Malcev, 2019-02-01 15:43:20)

Re: AHK: Перехват com интерфейса процесса

Хорошая информация по теме:
https://vedmysh.livejournal.com/8984.html
Непонято, Minhook.dll криво работает или autohotkey.dll его криво инжектит?