AhkHook: API hooking with autohotkey

Post your working scripts, libraries and tools
Peixoto
Posts: 23
Joined: 18 Aug 2015, 11:20

AhkHook: API hooking with autohotkey

29 Aug 2015, 20:57

If you came looking for the game patches have created with the program, go here:
http://www.vogons.org/viewtopic.php?f=24&t=53121

If you are interested in writing API hooks with autohotkey, download AhkHook from here:
https://github.com/Daniel-Lobo/AhkHook and ask questions in this topic

AhkHook sample:

Code: Select all

#NoEnv
SetWorkingDir %A_ScriptDir%  
#SingleInstance Ignore

if ! A_isdll
{
	target=%1%
	if not target 
	{
		msgbox, 16, , Drag and drop the taget application over the script !
		ExitApp
	}
	
	run ..\ahkhook.exe -t "%target%" -s "%A_scriptfullpath%" -a """"commandline argument with quotes"""" -e "settings test", , ,ProcessID	
	return
}


envget, my_settings, remote_settings
msgbox % "Testing the ""-e"" command, the string passed was: """ my_settings """"
global msgbox_hook := new Hook("MessageBoxW", "User32.dll", "MessageBoxW")

MessageBoxW(p1, p2, p3, p4)
{
	r := dllcall(msgbox_hook.Trampoline, uint, p1, uint, p2, str, "This Messagebox was hooked", uint, p4)	
	/* Unhook a function just by deleting the hook object. You will note that in the supplyed MesageBox test propgram, 
	 * only the first message box is hooked
	 */
	msgbox_hook := ""
	return r
}
Last edited by Peixoto on 28 Mar 2017, 19:10, edited 15 times in total.
GeekDude
Posts: 853
Joined: 02 Oct 2013, 22:13

Re: AHK Injector: DirectX hooking with autohotkey

29 Aug 2015, 21:02

Peixoto wrote:I will post it in a minute, just wanted the thread address to add to the documentation
And now the post's original contents have been preserved for legacy purposes.
guest3456
Posts: 2685
Joined: 09 Oct 2013, 10:31

Re: AHK Injector: DirectX hooking with autohotkey

29 Aug 2015, 23:50

i don't see any injector source nor .dll source

all i see are some game specific scripts

Peixoto
Posts: 23
Joined: 18 Aug 2015, 11:20

Re: AHK Injector: DirectX hooking with autohotkey

30 Aug 2015, 00:57

guest3456 wrote:i don't see any injector source nor .dll source

all i see are some game specific scripts
Forgive-me by the omission, it has been added
guest3456
Posts: 2685
Joined: 09 Oct 2013, 10:31

Re: AHK Injector: DirectX hooking with autohotkey

30 Aug 2015, 05:14

excellent thank you, i need to study this

Peixoto
Posts: 23
Joined: 18 Aug 2015, 11:20

Re: AHK Injector: DirectX hooking with autohotkey

01 Sep 2015, 16:24

evilC wrote:Do you know anything about the feasibility of doing this with later DirectX versions?
Would love to have generic OSD code that worked with any game...
I've have done a modification (included in the program) in the pixel shaders in I'am alive (DirectX9)... and it worked

You can find code to write text over the DirectX9 backbuffer in D3Dx9.ahk\class d3DX9\mehod DrawText or to
draw textured flat rectangles in in d3D9.ahk\class FlatRect

DirectX is thread safe since forever (i believe)... so i think it really depends if the game does all the calls you plan on hooking from the same thread (seems to be the case for unreal engine 3) if it doesn't, Autohokey.dll crashes complaining that it's out of memory...

this can always e solved by using the /singlecore command, but for newer games this would cripple performance...

The solution (that i will try to implement eventually) could be ditch MS detours and write a hooking library that uses some windows synchronization API like mutexes before calling the AHK callback. This call would probably lock other treads but would impact performance less then forcing a singe core...
Peixoto
Posts: 23
Joined: 18 Aug 2015, 11:20

Re: AHK Injector: DirectX hooking with autohotkey

18 Sep 2015, 19:57

I've included a few samples showing how to hook winapi functions or DirectX methods with about 30 lines of code
MrB

Re: AHK Injector: DirectX hooking with autohotkey

27 Oct 2015, 04:45

Thanks for the tool. I've want to use it to play TR2 and it starts and everything looks good.
The only thing is the controller is not working. I'm using an xbox one controller plugged in via USB. Its set as the main controller.
The only buttons that work ingame is the dpad and left analogue stick. Both move Lara around but none of the other buttons work. Is there someone I need to do?
Peixoto
Posts: 23
Joined: 18 Aug 2015, 11:20

Re: AHK Injector: DirectX hooking with autohotkey

28 Oct 2015, 18:43

MrB wrote:Thanks for the tool. I've want to use it to play TR2 and it starts and everything looks good.
The only thing is the controller is not working. I'm using an xbox one controller plugged in via USB. Its set as the main controller.
The only buttons that work ingame is the dpad and left analogue stick. Both move Lara around but none of the other buttons work. Is there someone I need to do?
You haven't configured the controller buttons. You have to select the arrow keys in the game's main menu and configure the buttons...
if the dpad works, the controller part of the script is working
Jonte999770
Posts: 1
Joined: 23 Mar 2016, 16:49

Re: AHK Injector: DirectX hooking with autohotkey

23 Mar 2016, 16:52

Good script but i have a small problem.
I'm trying to run RE1 on PC with this and since i get the error "Failed to initialize the Graphic Hardware Device.(3)" i need to use the /nocard1 command
but -args "/nocard1" doesn't work, i've tried using it everywhere in the script but i can't just get it to work.
I guess it would be the wrong command but it says in the help file that "-args informs the command line arguments that should be passed to the target application" so i don't know
(btw i'm using the script witch comes with the download just to clarify :P)
User avatar
HinkerLoden
Posts: 93
Joined: 23 Mar 2016, 07:50
GitHub: HinkerLoden

Re: AHK Injector: DirectX hooking with autohotkey

26 Mar 2016, 12:24

http://spazzarama.com/2011/03/14/c-scre ... api-hooks/

found this - is it possible to create a bridge between this old dx7 and this one.
Guest

Re: AHK Injector: DirectX hooking with autohotkey

03 Apr 2016, 10:41

Jonte999770 wrote:Good script but i have a small problem.
I'm trying to run RE1 on PC with this and since i get the error "Failed to initialize the Graphic Hardware Device.(3)" i need to use the /nocard1 command
but -args "/nocard1" doesn't work, i've tried using it everywhere in the script but i can't just get it to work.
I guess it would be the wrong command but it says in the help file that "-args informs the command line arguments that should be passed to the target application" so i don't know
(btw i'm using the script witch comes with the download just to clarify :P)
Peixoto
Posts: 23
Joined: 18 Aug 2015, 11:20

Re: AHK Injector: DirectX hooking with autohotkey

12 Apr 2016, 15:18

Sorry for the late replay, i didn't had internet access (except in my phone) for the last week
Jonte999770 wrote:Good script but i have a small problem.
I'm trying to run RE1 on PC with this and since i get the error "Failed to initialize the Graphic Hardware Device.(3)" i need to use the /nocard1 command
but -args "/nocard1" doesn't work, i've tried using it everywhere in the script but i can't just get it to work.
I guess it would be the wrong command but it says in the help file that "-args informs the command line arguments that should be passed to the target application" so i don't know
(btw i'm using the script witch comes with the download just to clarify :P)
Resident evil.bat already has the command -args /blaster. Replace it with -args /nocard1 (no quotes) and resident evil will run in software mode. There is no point however in running RE with AHK injector in software mode. AHK injector is about DirectX hooking, when you run resident evil in software, nothing the program does will work (except making the game recognize the triggers and dpad in the Xbox 360 controller).

The "Failed to initialize the Graphic Hardware Device.(3)" problem is something that probably could be fixed with AHK injector, but since i cant reproduce the bug, i can't fix it. You can use dgVoodoo2 (google it) to get rid of that error and the game will run well (provided that you configure dVoodoo2 to emulate the radeon 8500 card). There is still gonna be 2 problems: 1 - There will be a lighting bug that appears after you pick up an item. 2 - FMVs will only play if you use alt+enter to switch to windowed mode (that is possible using dgVoodoo2). I should be able to fix the last issue at the very least by forcing a borderless fullcreen window. I will include that fix in the next update as well as some code to monitor which call fails causing the "Failed to initialize the Graphic Hardware Device.(3)" error.
HinkerLoden wrote:http://spazzarama.com/2011/03/14/c-scre ... api-hooks/

found this - is it possible to create a bridge between this old dx7 and this one.
What exactly to you intent to accomplish with that ? Creating overlays with in DX7 games is already possible and simple with AHK injector. The download already includes a sample for a text overlay. In the next update, i will include a sample for bitmap overlays rendering directly to the render target (instead of bliting as i does today because bliting is slow on windosw 8 and 10)
SpecialGuest

Re: AHK Injector: DirectX hooking with autohotkey

12 Apr 2016, 17:01

Thought I'd mention the DX9 overlay API here since it's pretty handy as well : I remember using it to draw my own futuristic HUD on top of games
and to show the current time, fps, song playing (on webradio or locally) and what not.
predonix1

Re: AHK Injector: DirectX hooking with autohotkey

25 May 2016, 09:15

Resident Evil 1: failed to initialize the graphic hardware device. (3)
error.log:

Code: Select all

AHK Injector remote settings:
	args: /blaster
	blaster: 1
	compatlayer: win95
	controller: A=1;B=2;X=3;Y=4;LEFT_SHOULDER=5;RIGHT_SHOULDER=6;bLeftTrigger=3;bRightTrigger=8;start=8;back=9;LEFT_THUMB=10;RIGHT_THUMB=11;
	d3D: 1
	error_log: C:\Users\Jerry\Desktop\re1fix\AHK Injector\error.log
	frametime: 40.0
	injector_dir: C:\Users\Jerry\Desktop\re1fix\AHK Injector
	Mydocs: C:\Users\Jerry\Documents
	os_version: 10.000000
	script: Scripts\Resident Evil.ahk
	script_hwnd: 0x190616
	target: C:\Program Files (x86)\RESIDENT EVIL\USA\NEWEUR.EXE
	target_dir: C:\Program Files (x86)\RESIDENT EVIL\USA
	target_name: NEWEUR.EXE
	TextSwap: samples=4;thumbnail=256;path=C:\Users\Jerry\Documents\Games\ResidentEvil\Textures;next=pgup;prev=pgdn;dump=home;switch=end;color_switch=del;nopopins

Succeeded to create the DirectInput Interfaces
Succeeded to hook IDirectInputDeviceW::GetDeviceState
Desktop 1920x1080
DirectDrawCreate Hook:        0 <- 0 means no error
DirectDrawCreateEx Hook:      0 <- 0 means no error
DirectDrawEnumerateA Hook:    0 <- 0 means no error
DirectDrawEnumerateExA Hook:  0 <- 0 means no error
DirectDrawEnumerateW Hook:    0 <- 0 means no error
DirectDrawEnumerateExW Hook:  0 <- 0 means no error
DirectDrawCreateClipper_Hook: 0 <- 0 means no error
Succeeded to create DirectDraw
Succeeded to create Direct3D
Succeeded to hook IDirectDraw::QueryInterface
Succeeded to hook IDirectDraw2::CreateSurface
Succeeded to hook IDirectDraw::CreateSurface
Succeeded to hook IDirect3DDevice::EndScene
Succeeded to hook IDirect3DTexture::GetHandle
Succeeded to hook IDirect3DTexture::Release
Succeeded to hook IDirect3DDevice::BeginScene
failed to create surface 2147942487:  71 147456
Peixoto
Posts: 23
Joined: 18 Aug 2015, 11:20

Re: AHK Injector: DirectX hooking with autohotkey

27 May 2016, 21:44

predonix1 wrote:Resident Evil 1: failed to initialize the graphic hardware device. (3)
error.log:

Code: Select all

AHK Injector remote settings:
	args: /blaster
	blaster: 1
	compatlayer: win95
	controller: A=1;B=2;X=3;Y=4;LEFT_SHOULDER=5;RIGHT_SHOULDER=6;bLeftTrigger=3;bRightTrigger=8;start=8;back=9;LEFT_THUMB=10;RIGHT_THUMB=11;
	d3D: 1
	error_log: C:\Users\Jerry\Desktop\re1fix\AHK Injector\error.log
	frametime: 40.0
	injector_dir: C:\Users\Jerry\Desktop\re1fix\AHK Injector
	Mydocs: C:\Users\Jerry\Documents
	os_version: 10.000000
	script: Scripts\Resident Evil.ahk
	script_hwnd: 0x190616
	target: C:\Program Files (x86)\RESIDENT EVIL\USA\NEWEUR.EXE
	target_dir: C:\Program Files (x86)\RESIDENT EVIL\USA
	target_name: NEWEUR.EXE
	TextSwap: samples=4;thumbnail=256;path=C:\Users\Jerry\Documents\Games\ResidentEvil\Textures;next=pgup;prev=pgdn;dump=home;switch=end;color_switch=del;nopopins

Succeeded to create the DirectInput Interfaces
Succeeded to hook IDirectInputDeviceW::GetDeviceState
Desktop 1920x1080
DirectDrawCreate Hook:        0 <- 0 means no error
DirectDrawCreateEx Hook:      0 <- 0 means no error
DirectDrawEnumerateA Hook:    0 <- 0 means no error
DirectDrawEnumerateExA Hook:  0 <- 0 means no error
DirectDrawEnumerateW Hook:    0 <- 0 means no error
DirectDrawEnumerateExW Hook:  0 <- 0 means no error
DirectDrawCreateClipper_Hook: 0 <- 0 means no error
Succeeded to create DirectDraw
Succeeded to create Direct3D
Succeeded to hook IDirectDraw::QueryInterface
Succeeded to hook IDirectDraw2::CreateSurface
Succeeded to hook IDirectDraw::CreateSurface
Succeeded to hook IDirect3DDevice::EndScene
Succeeded to hook IDirect3DTexture::GetHandle
Succeeded to hook IDirect3DTexture::Release
Succeeded to hook IDirect3DDevice::BeginScene
failed to create surface 2147942487:  71 147456

Oh...thanks for the report... now we know the error happens when the game tries to create the z buffer, i'll see what i can do

Edit: uploaded a tentative fix
kalash49
Posts: 1
Joined: 05 Jun 2016, 19:18

Re: AHK Injector: DirectX hooking with autohotkey

05 Jun 2016, 21:27

The RE1 patch doesn't work properly long time ago on my Win7x64. In RE1-2 the latest builds launching with initialize error, it launching successfully only after set up custom resolution, but it works without scaling and with displaced video playing.
Screens RE1-2
In default settings it crush with failed to initialize the graphic hardware device. (3) error. Judging by the error.log the reason is it wrongly detects desktop resolution. The latest build with properly working scaling in RE1 was before you added Dino Crisis. Before DC build it launches with scaling only with -d3D 2 parameter but fps control not works. With default settings: initialize error.
Error logs RE1
Peixoto
Posts: 23
Joined: 18 Aug 2015, 11:20

Re: AHK Injector: DirectX hooking with autohotkey

07 Jun 2016, 08:11

kalash49 wrote:The RE1 patch doesn't work properly long time ago on my Win7x64. In RE1-2 the latest builds launching with initialize error, it launching successfully only after set up custom resolution, but it works without scaling and with displaced video playing.
Screens RE1-2
In default settings it crush with failed to initialize the graphic hardware device. (3) error. Judging by the error.log the reason is it wrongly detects desktop resolution. The latest build with properly working scaling in RE1 was before you added Dino Crisis. Before DC build it launches with scaling only with -d3D 2 parameter but fps control not works. With default settings: initialize error.
Error logs RE1
I've fixed the desktop resolution detection, but unfortunately no idea about you problem with RE1
MechanicalPaladin
Posts: 2
Joined: 07 Aug 2016, 05:19

Re: AHK Injector: DirectX hooking with autohotkey

07 Aug 2016, 05:23

Just wanted to say thank you very much for this!
I've been begging for Dino Crisis widescreen patch for quite some time now and you did it!
I have a couple questions.

In Dino Crisis, why can't you walk forward and turn at the same time when using a controller but when playing with a keyboard you can do that just fine (↑→)?
Why can't you rebind controller keys?

Will there be support for Dino Crisis 2?

Is there a way to scale up the FMV videos in Resident Evil 1? Now they are centered and played in their original resolution (320x240) witch make them appear very small on a Full HD monitor.

Is it possible to use Steam overlay with this tool? I'm able to launch the game trough Steam but as soon as the command window closes, it no longer says I'm playing the game (Resident Evil)
The reason I ask this is because I want to take screenshots and talk to my friends without alt+tabbing out from the game.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: hoppfrosch and 52 guests