DllCall, DrawTextW Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jjones10
Posts: 6
Joined: 08 Sep 2017, 02:11

DllCall, DrawTextW

08 Sep 2017, 02:17

Hi, I'm trying to do something similar to this, but with AHK:

https://stackoverflow.com/questions/405 ... n-crashing

so far I have this, but its not working:

http://p.ahkscript.org/?p=63cf6d92

Any help would be appreciated
jjones10
Posts: 6
Joined: 08 Sep 2017, 02:11

Re: DllCall, DrawTextW

10 Sep 2017, 14:13

I will pay someone if they can help me
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: DllCall, DrawTextW

10 Sep 2017, 15:35

Why is ProcessID wrapped in %?
jjones10
Posts: 6
Joined: 08 Sep 2017, 02:11

Re: DllCall, DrawTextW

10 Sep 2017, 16:12

kczx3 wrote:Why is ProcessID wrapped in %?
I've tried it with % and without and neither works.
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: DllCall, DrawTextW

10 Sep 2017, 19:33

This is not working, but thought I would share it anyway.

Code: Select all

#SingleInstance, force

WH_CALLWNDPROC := 4
WH_CALLWNDPROCRET := 12

libraryHandle := DllCall("LoadLibrary", "Str", "user32.dll", "Ptr")
DrawTextWAddress := DllCall("GetProcAddress", "Ptr", libraryHandle, "AStr", "DrawTextW", "Ptr")

TestHook := DllCall("SetWindowsHookEx", "Int", WH_CALLWNDPROC, "Ptr", DrawTextWAddress, "Ptr", libraryHandle, "Ptr", 0)
MsgBox, % TestHook
DllCall("UnhookWindowsHookEx", "Ptr", TestHook)

CallBack := RegisterCallback("TestProc", "Fast")
ThisLibAddress := DllCall("GetModuleHandle", "Ptr", 0)

TestHook := DllCall("SetWindowsHookEx", "Int", WH_CALLWNDPROC, "Ptr", CallBack, "Ptr", ThisLibAddress, "Ptr", 0)
MsgBox, % TestHook
DllCall("UnhookWindowsHookEx", "Ptr", TestHook)
return

TestProc(nCode, wParam, lParam) {
	Critical

	ToolTip, % lParam

	return DllCall("CallNextHookEx", "Ptr", 0, "Int", nCode, "Ptr", wParam, "Ptr", lParam)
}
The second try is based on this code

Code: Select all

#Persistent
#SingleInstance, force

if(A_PtrSize != 4) {
	SplitPath, % A_AHKPath, , folder
	Run, % folder "\AutoHotkeyU32.exe " """" A_ScriptFullPath """"
	ExitApp
}

OnExit, Unhook

WH_KEYBOARD_LL := 13
WH_MOUSE_LL := 14

hHookKeybd := SetWindowsHookEx(WH_KEYBOARD_LL, RegisterCallback("Keyboard", "Fast"))
hHookMouse := SetWindowsHookEx(WH_MOUSE_LL, RegisterCallback("MouseMove", "Fast"))
Return

Unhook:
	UnhookWindowsHookEx(hHookKeybd)
	UnhookWindowsHookEx(hHookMouse)
ExitApp

Keyboard(nCode, wParam, lParam) {
	Static WM_KEYDOWN := 0x100, WM_KEYUP := 0x101
	Critical
	SetFormat, Integer, H
	if(wParam = WM_KEYDOWN || wParam = WM_KEYUP) {
		KeyName := GetKeyName("vk" NumGet(lParam+0, 0))
		Tooltip, % (wParam = 0x100) ? KeyName " Down" : KeyName " Up"
	}
	Return CallNextHookEx(nCode, wParam, lParam)
}

MouseMove(nCode, wParam, lParam) {
	Critical
	SetFormat, Integer, D
	if(!nCode && (wParam = 0x200)) {
		Tooltip, %  "X " NumGet(lParam+0, 0, int) " Y " NumGet(lParam+0, 4, int)
	}
	Return CallNextHookEx(nCode, wParam, lParam)
}

SetWindowsHookEx(idHook, pfn) {
	Return DllCall("SetWindowsHookEx", "Int", idHook, "Ptr", pfn, "Ptr", DllCall("GetModuleHandle", "Ptr", 0), "Ptr", 0)
}

UnhookWindowsHookEx(hHook) {
	Return DllCall("UnhookWindowsHookEx", "Ptr", hHook)
}

CallNextHookEx(nCode, wParam, lParam, hHook = 0) {
	Return DllCall("CallNextHookEx", "Ptr", hHook, "Int", nCode, "Ptr", wParam, "Ptr", lParam)
}
Please excuse my spelling I am dyslexic.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: DllCall, DrawTextW

11 Sep 2017, 02:35

Capn odin, for the hook setups, try to replace DllCall("GetModuleHandle", "Ptr", 0) with 0. If the problem is with the hooks that is. I'm on the phone so typing and reading is awkward.
Edit
SetWindowsHookEx wrote:The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.
Cheers.
jjones10
Posts: 6
Joined: 08 Sep 2017, 02:11

Re: DllCall, DrawTextW

17 Sep 2017, 03:52

Thank you. I will try these ideas.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: DllCall, DrawTextW  Topic is solved

17 Sep 2017, 10:36

This will never work because:
  • SetWindowsHookEx isn't designed for hooking arbitrary functions; there's a reason why libraries like EasyHook (mentioned in the SO link in your first post) exist
  • A WH_CALLWNDPROC hook procedure is meant to be located in the process itself. If it's in a DLL, Windows loads it into the remote process for you. With an AutoHotkey script such as one in your example, it exists only in AutoHotkey.exe and so Notepad.exe will never even look at it
I have an extremely contrived example of scripts here that load AutoHotkey_H.dll into Notepad and sets up the excellent MinHook library to hijack lstrcmpW. Remove the SetWindowsHookEx crap from reciever.ahk, the "DllCall("IsTopLevelWindow", "Ptr", hWnd, "Ptr")" line (every window from a shellhook is toplevel, duh me), and (DllCall("GetModuleHandleExW", "UInt", GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT := 0x00000002, "WStr", "AutoHotkeyMini.dll", "Ptr*", hInstance) (and references to hInstance) can be replaced with A_ModuleHandle.

You might find this AhkHook project to be far more simple.

If all fails, just write a DLL that uses MinHook in C. This is my favourite way as it's the only way that gives me full control of everything from top to bottom, which I need for ways to have my DLL automatically loaded by a remote process without manual injection (making a proxy DLL that assumes the name of another non-KnownDLL DLL? Great, just export the functions of the original DLL and forward them. Using a fake Application Verifier DLL? Great, you can actually handle the "DLL_PROCESS_VERIFIER" condition in DllMain yourself. Your target program links to user32.dll and you want to use AppInit_DLLs? Great, you're actually able to use DllMain to do what you want. [I'd love to be proven wrong here, but I think AutoHotkey_H.dll's mechanism for automatically loading a certain-named script file is disabled, meaning it can't be used in that situation]).
jjones10
Posts: 6
Joined: 08 Sep 2017, 02:11

Re: DllCall, DrawTextW

19 Sep 2017, 07:27

Thank you very much. I will look into those ideas.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, prototype_zero, w_i_k_i_d and 339 guests