how to dll Inject Topic is solved

Ask for help, how to use AHK_H, etc.
whynotregister
Posts: 61
Joined: 05 Nov 2016, 22:42

how to dll Inject

18 Jan 2017, 13:56

https://github.com/kevrgithub/autohotke ... ectDll.ahk

I tested it with the script in the link above, but it does not work.
Is there a simple way to do dll injection with ahk?
User avatar
jeeswg
Posts: 2213
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to dll Inject

18 Jan 2017, 18:42

Is dll injection an 'OK' thing to do?

Like, if I were to say on Stack Overflow, you can change the font size and control colours in legacy programs using dll injection, would there be a problem?

Or similarly if I said you can retrieve the file path and set the three (yes, three) colours in MS Paint (Windows XP) by searching/editing the address space, would there be a problem?

'These are totally hypothetical things that may or may not be possible.'
Last edited by jeeswg on 17 Aug 2017, 15:24, edited 1 time in total.
FanaticGuru
Posts: 710
Joined: 30 Sep 2013, 22:25

Re: how to dll Inject

18 Jan 2017, 19:46

jeeswg wrote:Is dll injection an OK thing to do?
I am not sure what you mean by is it ok or a problem. Some programs it works fine, some it don't.
Some programs like online games that monitor for tampering especially don't like it.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
whynotregister
Posts: 61
Joined: 05 Nov 2016, 22:42

Re: how to dll Inject

19 Jan 2017, 06:03

HotKeyIt wrote:Try InjectAhkDll()



#include _Struct.ahk
#include sizeof.ahk
InjectAhkDll(PID,dll="AutoHotkey.dll",script=0){
static PROCESS_ALL_ACCESS:=0x1F0FFF,MEM_COMMIT := 0x1000,MEM_RELEASE:=0x8000,PAGE_EXECUTE_READWRITE:=64
,hKernel32:=DllCall("LoadLibrary","Str","kernel32.dll","PTR"),LoadLibraryA:=DllCall("GetProcAddress","PTR",hKernel32,"AStr","LoadLibraryA","PTR")
,base:={__Call:"InjectAhkDll",__Delete:"InjectAhkDll"},FreeLibrary:=DllCall("GetProcAddress","PTR",hKernel32,"AStr","FreeLibrary","PTR")
static TH32CS_SNAPMODULE:=0x00000008,INVALID_HANDLE_VALUE:=-1
,MAX_PATH:=260,MAX_MODULE_NAME32:=255,ModuleName:="",init:=VarSetCapacity(ModuleName,MAX_PATH*(A_IsUnicode?2:1))
,_MODULEENTRY32:="
(
DWORD dwSize;
DWORD th32ModuleID;
.
.
.
Process,wait, notepad.exe
winget,pid,pid,ahk_exe notepad.exe
rThread:=InjectAhkDll(PID,A_ScriptDir "\AutoHotkeyMini.dll","#Persistent`nMsgBox % A_WorkingDir")
Sleep 500
rThread.Exec("MsgBox % A_AhkPath")


The program terminates with a crash.
Compiled from x64 to x86 bin.
HotKeyIt
Posts: 1502
Joined: 29 Sep 2013, 18:35
Contact:

Re: how to dll Inject  Topic is solved

19 Jan 2017, 12:35

Try this AHK_H version:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

whynotregister
Posts: 61
Joined: 05 Nov 2016, 22:42

Re: how to dll Inject

19 Jan 2017, 13:56

HotKeyIt wrote:Try this AHK_H version:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus





It works really well. Thank you. :D :D
User avatar
jeeswg
Posts: 2213
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to dll Inject

18 Aug 2017, 20:10

Is it possible to use ComObjCreate and InjectAhkDll() in AutoHotkey_L v1.1?

I'm getting the error message:
An outgoing call cannot be made since the application is dispatching an input-synchronous call.


Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

HotKeyIt
Posts: 1502
Joined: 29 Sep 2013, 18:35
Contact:

Re: how to dll Inject

19 Aug 2017, 03:37

Exec works via SendMessage and while this is being processed you cannot use COM calls.
User avatar
jeeswg
Posts: 2213
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to dll Inject

19 Aug 2017, 08:01

Thank you HotKeyIt, but this potentially scuppers a lot of things I might want to do with dll injection, is there any workaround?

3 potential uses for dll inject and COM:

[major: drag-and-drop a file to a program when WM_DROPFILES doesn't work]
[COM] Help with the IDropSource and IDropTarget interfaces - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=8700&p=164860#p164860

[middle: Common Item Dialog get folder/file]
get full paths of selected files on Desktop and Common File Dialogs - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=31135&p=145577#p145577

[minor: Explorer get column widths in Windows 7]
Explorer column interaction (get/set: which appear, width, ascending/descending order) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=33129&p=154461#p154461
Last edited by jeeswg on 19 Aug 2017, 08:44, edited 5 times in total.
HotKeyIt
Posts: 1502
Joined: 29 Sep 2013, 18:35
Contact:

Re: how to dll Inject

19 Aug 2017, 08:13

You have to include the script upfront when calling InjectAhkDll and control it using .Exec("SetTimer, ...").
User avatar
jeeswg
Posts: 2213
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to dll Inject

19 Aug 2017, 08:48

@HotKeyIt: Many thanks, I had thought that SetTimer might be a way, but I don't really understand InjectAhkDll. Btw when I ran the script, I used SetTimer with '-1', but it appeared to run the subroutine twice simultaneously (I got two MsgBoxes).

Btw also, what is the best way to cleanly withdraw, i.e. 'uninject'. I've been experimenting with .Terminate and ExitApp but I'm still getting crashes regularly. Is there any documentation or advice for this function somewhere? Cheers.
HotKeyIt
Posts: 1502
Joined: 29 Sep 2013, 18:35
Contact:

Re: how to dll Inject

19 Aug 2017, 10:00

Did you include the script with Return in first line when calling InjectAhkDll? .Exec() deletes the script after it ran so you can't use it to create script and SetTimer!
User avatar
jeeswg
Posts: 2213
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to dll Inject

19 Aug 2017, 10:21

Haha cheers, I didn't understand what you meant at first, but I worked it out after, helped by your comment, when I checked over the code. My code works now, I get one MsgBox, now that I added 'return' to the top of my vScriptFunc variable.

Re. crashing. If I do q it works, and then I can do w as many times as I like, but if I do e to try and close the thread, and then q to inject again, it crashes.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

HotKeyIt
Posts: 1502
Joined: 29 Sep 2013, 18:35
Contact:

Re: how to dll Inject

19 Aug 2017, 14:47

Did you rThread:="" before injecting again?
User avatar
jeeswg
Posts: 2213
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to dll Inject

19 Aug 2017, 16:35

You're quite right HotKeyIt.
This seems to be invalid:
rThread.Terminate()
I replaced it with this and it's all working:
rThread := ""

All the details for the function are here it turns out:
[SOLVED]get other process's working dir - Page 3 - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/85304-solvedget-other-processs-working-dir/page-3#entry544650

A small amount of documentation, but all that's needed.

==================================================

One thing that someone mentioned to me, was that some dll injection techniques require you to latch onto a specific thread, rather than simply to latch onto a process.

Now, I don't actually need to do that, as I have workarounds, however, I'd just like to mention it in case you have any useful links, or anything to say on how to achieve that. Cheers.

[EDIT:] At some point soon I will post some example code for setting the font colour/background colour/font in Notepad, and a prototype window message spy.
HotKeyIt
Posts: 1502
Joined: 29 Sep 2013, 18:35
Contact:

Re: how to dll Inject

19 Aug 2017, 18:03

Here CreateRemoteThread is used so a new thread is created in process, not sure how to latch onto a specific thread and when / why it might be required.
User avatar
jeeswg
Posts: 2213
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to dll Inject

19 Aug 2017, 18:15

qwerty12 provided an example of latching onto a specific thread here, using Tiny C Compiler:
[Common Item Dialog get folder/file]
get full paths of selected files on Desktop and Common File Dialogs - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=31135&p=145577#p145577

And mentioned another potential use of it here:
[Explorer get column widths in Windows 7]
Explorer column interaction (get/set: which appear, width, ascending/descending order) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=33129&p=154461#p154461

These two links also appear earlier on this page. Both problems have workarounds, however, maybe one day the approach could be very useful. Cheers.
User avatar
jeeswg
Posts: 2213
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to dll Inject

23 Aug 2017, 16:30

I have a prototype window spy, but it seems to crash the program it latched onto when I: reload the script, end the remote thread, or try to close the program.

WARNING: use with caution, *expect* it to crash the program. This will crash your program.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 2213
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to dll Inject

23 Aug 2017, 18:50

Here are some examples for interacting with Notepad, which is the last dll injection script I intend to post, unless I can ever get this working:
[COM] Help with the IDropSource and IDropTarget interfaces - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=8700&p=165994#p165994

I'm still having problems relating to dll injection and programs crashing, if anyone can help me with that.

The main aim here is to programmatically set the background colour and font for Notepad's Edit control.

Note: The default system colour for Edit controls can be changed (e.g. on Windows 7) by going to Control Panel\Appearance and Personalization\Personalization and changing the colour for 'Window'. Or you can get/set that colour programmatically by using GetSysColor/SetSysColors (note: 'Color' v. 'Colors').

This script changes the colour for an individual existing Edit control.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Note in terms of scripts for setting the colour of an Edit control, there are a tonne of links if you search for 'edit control set colour' or 'Control_Colors', here are some of them:

Spoiler
Last edited by jeeswg on 25 Aug 2017, 11:47, edited 1 time in total.

Return to “Ask For Help”

Who is online

Users browsing this forum: No registered users and 1 guest